Zooky
Zooky

Reputation: 133

How to construct a js object and call its methods with js_of_ocaml?

I try to build an application using js_of_ocaml. Let's say I have the following code in javascript :

function MyFunction(){ this.name = "a" }
MyFunction.prototype.setName = function(arg){ this.name = arg }

How to write in OCaml/js_of_caml a code that will have the same effect as the js code below ?

val myInstance = new MyFunction();
myInstance.setName("Joe");

I tried to write something like :

let onload _ =
  let myInstance = Js.Unsafe.new_obj (Js.Unsafe.variable "MyFunction") [||] in
  Js.Unsafe.call (Js.Unsafe.variable "MyFunction.prototype.setName") myIntance [|Js.Unsafe.inject "Joe"|];
  Js._false ;;
let _ = Html.window##onload <- Html.handler onload;

The constructor is called but I have errors following so it doesn't seem to be the right way to do it. I have also tried the Js.Unsafe.meth_call function but it's not better.

Any help would be appreciated.

Upvotes: 1

Views: 939

Answers (1)

hhugo
hhugo

Reputation: 241

Your Unsafe version should work with

let onload _ =
  let myInstance = Js.Unsafe.new_obj (Js.Unsafe.variable "MyFunction") [||] in
  Js.Unsafe.meth_call myInstance "setName" [|Js.Unsafe.inject (Js.string "Joe")|];
  Js._false ;;
 let _ = Dom_html.window##onload <- Dom_html.handler onload;

it generate

function _A_(_a_){new MyFunction().setName("Joe");return _M_}

A safer (typed) version would be

class type myFun = object
  method setName : Js.js_string Js.t -> unit Js.meth
end
let myFun : myFun Js.t Js.constr =
  Js.Unsafe.global##_MyFunction (* same as Js.Unsafe.variable "MyFunction" *)

let onload _ =
  let myInstance = jsnew myFun () in
  myInstance##setName(Js.string "Joe");
  Js._false ;;
let _ = Dom_html.window##onload <- Dom_html.handler onload;

it generate the following JavaScript

var _Q_=_d_.MyFunction;
function _A_(_a_){new _Q_().setName("Joe");return _M_}
_d_.onload=  .... _A_(..)

Upvotes: 3

Related Questions