this
Dart 関数からJavaScript オブジェクトにアクセスする必要があります。Dart-JS 相互運用機能を使用して、JavaScript オブジェクトに新しいメソッドを効果的に追加しています。Dart で定義されたメソッドから JavaScript オブジェクトにあるプロパティにアクセスする必要があります。
1 に答える
5
Callback
コンストラクターは from JavaScript を渡すことができますthis
。CallbackのAPIドキュメントによると:
new Callback.many(Function f, {bool withThis: false})
new Callback.once(Function f, {bool withThis: false})
次に例を示します。
ダーツコード:
import 'dart:html';
import 'package:js/js.dart' as js;
void main() {
var greeter = js.context['greeter'];
var msg = greeter['greet']('Bob');
greeter['doCoolStuff'] = new js.Callback.many(doCoolStuff, withThis: true);
}
doCoolStuff(jsThis) {
print(jsThis['msg']);
}
Notice the use of withThis: true
when creating the Callback. The this
from JavaScript is passed in as the first argument to the callback function. In this case, I give it a name of jsThis
.
JavaScript code:
function Greeter() {
this.msg = 'hello';
var that = this;
document.getElementById('clickme').addEventListener('click', function() {
that.doCoolStuff();
});
}
Greeter.prototype.greet = function(name) {
return this.msg + ' ' + name;
}
var greeter = new Greeter();
document.getElementById('clickme').addEventListener('click', function() {
greeter.doCoolStuff(); // comes from Dart land
});
于 2013-07-17T05:26:29.113 に答える