Skip to content

Latest commit

 

History

History
57 lines (44 loc) · 1.18 KB

property-access.md

File metadata and controls

57 lines (44 loc) · 1.18 KB
title
Property access

Static property access

Binding to Getter/Setter using bs.get, bs.set.

This attribute helps get and set the property of a JavaScript object.

type textarea
external set_name : textarea -> string -> unit = "name" [@@bs.set]
external get_name : textarea -> string = "name" [@@bs.get]
type textarea;
[@bs.set] external set_name: (textarea, string) => unit = "name";
[@bs.get] external get_name: textarea => string = "name";

Dynamic property access

Binding to dynamic property access/set using bs.set_index, bs.get_index

Input:

type t
external create : int -> t = "Int32Array" [@@bs.new]
external get : t -> int -> int = "" [@@bs.get_index]
external set : t -> int -> int -> unit = "" [@@bs.set_index]

let _ =
  let i32arr = (create 3) in
  set i32arr 0 42;
  Js.log (get i32arr 0)
type t;
[@bs.new] external create: int => t = "Int32Array";
[@bs.get_index] external get: (t, int) => int = "";
[@bs.set_index] external set: (t, int, int) => unit = "";
let i32arr = create(3);
i32arr->set(0, 42);
Js.log(i32arr->get(0));

Output:

var i32arr = new Int32Array(3);
i32arr[0] = 42;
console.log(i32arr[0]);