Warning: Reason support is experimental. We are looking for beta-tester and contributors.

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 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
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:

< foo : int -> unit Js.meth;
    x : int Js.readonly_prop;
    y : int Js.prop
> Js.t