我正在研究 Actionscript 3.0到Javascript - 转换器 我试图找到一种方法来解决定义顺序问题,因为它在AS中无关紧要,首先定义哪个包。令我惊讶的是,以下代码有效,尽管 A 的原型是在 BA 继承自 A 之后定义的:
<小时/> javascript(示例输出)
Function.prototype.inherit = function(base) {
var empty = Function.prototype.inherit.empty;
empty.prototype = base.prototype;
this.prototype = new empty();
}
Function.prototype.inherit.empty = function() {};
//order doesn't matter
(function(undefined) {
function BA() {}
BA.inherit(A);
BA.prototype.f1 = function() { return "success"; }
function A() {}
A.prototype.f1 = function() { return "fail"; }
A.prototype.f2 = function() { return "success"; }
var bA = new BA;
console.log("test1: %s; %s", bA.f1(), bA.f2());
console.assert(bA instanceof BA);
console.assert(bA instanceof A);
})();
今天我开始测试子类型,不幸的是,定义的顺序在这种情况下起作用:
<小时/> javascript(示例输出)
//order plays a decisive role
(function(undefined) {
function CBA() {}
CBA.inherit(BA);
CBA.prototype.constructor = CBA;
CBA.prototype.f3 = function() { return "success"; }
function BA() {}
BA.inherit(A);
BA.prototype.constructor = BA;
BA.prototype.f1 = function() { return "success"; }
function A() {}
A.prototype.f1 = function() { return "fail"; }
A.prototype.f2 = function() { return "success"; }
var cBA = new CBA;
//this won't work. The compiler claims that cBA.f1 is not a function
console.log("test2: %s; %s, %s", cBA.f1(), cBA.f2(), cBA.f3());
})();
<小时/>
package my.pkg1 {
class A {
}
class B extends my.pkg2.C {
}
package my.pkg2 {
class C {
}
class D extends my.pkg1.A {
}
}
感谢。
乌利