2
我有一個使用CreateJS js庫製作的HTML5遊戲。我想用Dart重寫它,但我的大多數對象都繼承自CreateJS對象(例如Container)。我能夠保存這種繼承嗎?使用Dart與其他js庫設計用於簡化在畫布上的繪製有什麼好方法嗎?從JS類繼承Dart類?
我有一個使用CreateJS js庫製作的HTML5遊戲。我想用Dart重寫它,但我的大多數對象都繼承自CreateJS對象(例如Container)。我能夠保存這種繼承嗎?使用Dart與其他js庫設計用於簡化在畫布上的繪製有什麼好方法嗎?從JS類繼承Dart類?
Dart類不能直接擴展一個Javascript類。但是,您可以通過設置將執行Dart代碼的方法來自定義JavaScript對象。
例如,讓我們假設你有一個擴展Container
類Child
Javascript類:
function Container(){}
Container.prototype.callSayHello = function(){ this.sayHello(); }
Container.prototype.sayHello = function(){ alert("hello from JS"); }
function Child(){}
Child.prototype = new Container();
在省道邊,你可以創建一個Child
,並在其上覆蓋sayHello
從Container
定義sayHello
方法:
import 'dart:html';
import 'package:js/js.dart' as js;
main(){
// with sayHello overriding
js.scoped((){
final child = new js.Proxy(js.context.Child);
child.sayHello = new js.Callback.many(() {
window.alert("hello from Dart");
});
child.callSayHello(); // displays "hello from Dart"
});
// without sayHello overriding
js.scoped((){
final child = new js.Proxy(js.context.Child);
child.callSayHello(); // displays "hello from JS"
});
}
謝謝,它可能會幫助我。 – soin08