1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
= Camlp4 syntax extension for Js_of_ocaml
WARNING: The Camlp4 syntax extension is not longer part of the js_of_ocaml distribution.
A Camlp4 syntax extension is available for manipulating object properties,
invoking methods and creating objects.
We advise to use the <<a_manual chapter="ppx" |Ppx>> syntax extension instead.
The syntax and typing rules are as follows:
* Getting a property
{{{
obj : <m : u prop> Js.t
-----------------------
obj##m : u
}}}
* Setting a property
{{{
obj : <m : u prop> Js.t
e : u
-----------------------
obj##m <- e : unit
}}}
* Invoking a method
{{{
obj : <m : t_1 -> ... -> t_n -> u meth; ..> Js.t
e_i : t_i (1 <= i <= n)
-------------------------------------------------
obj##m(e_1, ..., e_n) : u
}}}
* Using an object constructor
{{{
constr : (t_1 -> ... -> t_n -> u Js.t) Js.constr
e_i : t_i (1 <= i <= n)
------------------------------------------------
jsnew constr (e1, ..., en) : u Js.t
}}}
* Creating a literal object
<<code language="ocaml"|
jsobject (self) (* Equivalent of this *)
val x = 3 (* read-only prop *)
val mutable y = 4 (* read/write prop *)
method foo i = self##y <- self##x + i
end
>>
Properties are defined with the [val] keyword. [mutable] makes the
property writable. [self] can be any identifier and will be bind
to [this] in javascript.
In this case, the object has the following type:
<<code language="ocaml"|
< foo : int -> unit Js.meth;
x : int Js.readonly_prop;
y : int Js.prop
> Js.t
>>
|