2014-02-21 106 views
5

我想從Javascript調用飛鏢功能。如何從Javascript調用Dart函數?

我想使用dart2js(版本1.1.3)編譯包含Dart函數的Dart腳本,然後將生成的.js文件加載到Javascript環境中,並從Javascript中調用該函數。

一起從下面的Javascript調用myHyperSuperMegaFunction的東西線。

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文件到瀏覽器。可變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]

回答

5

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

js.context['myHyperSuperMegaFunction'] = myHyperSuperMegaFunction; 

爲了您的信息new js.JsFunction.withThis有可能被用來當你需要使用this的js上下文。在你的錯誤,你可以看到,第一個參數是Instance of 'Window'這是JS中的全球背景。