1

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.

Zooky
  • 133
  • 4
  • 2
    Have you tried `Using a JS constructor` from http://ocsigen.org/js_of_ocaml/manual/bindings ? – Kakadu Mar 21 '14 at 10:28
  • Yes, I think that a Js.constr is passed to new_obj in the code that I posted. Actually, it seems that I made a mistake and that the code posted does work correctly (sorry). But I still don't understand the difference between Js.Unsafe.call and Js.Unsafe.meth_call (which does not work in this case). Is in my example "MyFunction.prototype.setName" not a "method" of the created object ? – Zooky Mar 21 '14 at 10:47

1 Answers1

3

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_(..)
hhugo
  • 241
  • 1
  • 3