如何从Javascript调用Dart函数?

mid*_*azz 5 dart dart2js

我想从Javascript调用Dart函数.

我想使用dart2js(版本1.1.3)编译包含Dart函数的Dart脚本,然后将生成的.js文件加载到Javascript环境中并从Javascript调用该函数.

myHyperSuperMegaFunction从Javascript下面调用的东西.

import 'dart:js' as js;

int myHyperSuperMegaFunction(int a, int b) {
  return a + b;
}

main() {
  js.context['myHyperSuperMegaFunction'] = new js.JsFunction.withThis(myHyperSuperMegaFunction);
}
Run Code Online (Sandbox Code Playgroud)

我尝试编译上面的内容dart2js并将生成的.js文件加载到Chrome中.变量myHyperSuperMegaFunction已注册并定义为

function () {
    return _call(f, captureThis, this, Array.prototype.slice.apply(arguments));
}
Run Code Online (Sandbox Code Playgroud)

但是,当我myHyperSuperMegaFunction(2,3)从Chrome Javascript控制台调用时,出现以下错误NoSuchMethodError : method not found: 'Symbol("call")' Receiver: Instance of '(){this.$initialize' Arguments: [Instance of 'Window', 2, 3]

Ale*_*uin 5

你不需要使用new js.JsFunction.withThis.在你的情况下,只需使用:

js.context['myHyperSuperMegaFunction'] = myHyperSuperMegaFunction;
Run Code Online (Sandbox Code Playgroud)

new js.JsFunction.withThis当您需要使用thisJs上下文时,必须使用您的信息.在您的错误中,您可以看到第一个参数是Instance of 'Window'Js中的全局上下文.