javascript - How to construct a js object and call its methods with js_of_ocaml? -
i try build application using js_of_ocaml. let's have following code in javascript :
function myfunction(){ this.name = "a" } myfunction.prototype.setname = function(arg){ this.name = arg }
how write in ocaml/js_of_caml code have same effect js code below ?
val myinstance = new myfunction(); myinstance.setname("joe");
i tried write :
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 called have errors following doesn't seem right way it. have tried js.unsafe.meth_call function it's not better.
any appreciated.
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 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 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 following javascript
var _q_=_d_.myfunction; function _a_(_a_){new _q_().setname("joe");return _m_} _d_.onload= .... _a_(..)
Comments
Post a Comment