我正在尝试编写编译成这个JS的ReasonML:
function main(example) {
example.foo = function() {
console.log(this)
}
}我的理由是:
let module Example = {
type t;
external set_foo_method : t => (t => unit [@bs.this]) => unit = "foo" [@@bs.set];
};
let main = fun example => Example.set_foo_method example (fun [@bs.this] x => {
Js.log(x);
});我在第二个[@bs.this]的行和列上得到一个语法错误。
File "/Users/maxwellheiber/dev/rerect/src/demo.re", line 6, characters 62-64:
Error: 742: <SYNTAX ERROR>我正在关注@bs.this的BuckleScript文档。
与OCaml相比,使用BuckleScript绑定this的语法在原因上是否不同?以下具有BuckleScript属性的OCaml (不是Reason)编译为正确的JS时不会出错:
module Example = struct
type t
external set_foo_method : t -> (t -> unit [@bs.this]) -> unit = "foo" [@@bs.set]
end
let main example = Example.set_foo_method example (fun [@bs.this] x -> Js.log(x))如何合理地使用[@bs.this] BuckleScript属性来生成使用this的JS
发布于 2017-07-28 02:37:43
是的,不幸的是,属性优先级和诸如此类的属性是微妙的不同。Reason Tools (它非常适合于转换像这样的小代码段)说这就是你想要的:
module Example = {
type t;
external set_foo_method : t => (t => unit) [@bs.this] => unit = "foo" [@@bs.set];
};
let main example => Example.set_foo_method example ((fun x => Js.log x) [@bs.this]);它将编译为
function main(example) {
example.foo = (function () {
var x = this ;
console.log(x);
return /* () */0;
});
return /* () */0;
}https://stackoverflow.com/questions/44893067
复制相似问题