我有一个使用CreateJS js库制作的HTML5游戏。我想使用Dart重写它,但我的大多数对象都继承自CreateJS对象(例如容器)。我能保存这样的遗产吗?有没有一种很好的方式将Dart与其他旨在简化画布上绘图的js库一起使用?
发布于 2012-12-23 06:11:36
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();在Dart端,您可以创建一个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"
});
}https://stackoverflow.com/questions/14000546
复制相似问题