我试图编写编译成这个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
的语法是否与Reason不同?以下带有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))
如何使用Reason中的[@bs.this]
BuckleScript属性生成使用this
的JS?
答案 0 :(得分:3)
是的,属性优先级,不幸的是,这种情况略有不同。 Reason Tools(这对于转换像这样的小片段很有用)说这就是你想要的:
DateTimeException
它将编译为
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]);