7

我想从 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);
}

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

function () {
    return _call(f, captureThis, this, Array.prototype.slice.apply(arguments));
}

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

4

1 回答 1

6

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

js.context['myHyperSuperMegaFunction'] = myHyperSuperMegaFunction;

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

于 2014-02-21T13:59:28.487 回答