派生类的构造函数返回基类的实例。
以下代码说明了我的问题:
// Vector is defined by an external module (Unreal.js)
class TestB extends Vector {
constructor() {
super();
}
Log() {
console.log("" + this);
}
}
console.log(new TestB() instanceof TestB) // returns false !!! why ???
console.log(new TestB() instanceof Vector) // returns true...
class TestA extends Array {
constructor() {
super();
}
Log() {
console.log("" + this);
}
}
console.log(new TestA() instanceof TestA); // returns true, all is good
这怎么可能?
似乎以Vector
使其与不兼容的方式实现了class
。
这是一个Vector
可以做到这一点的例子:
function Vector() {
var v = Object.create(Vector.prototype);
return v;
}
class TestB extends Vector {
constructor() {
super();
}
}
console.log(new TestB() instanceof TestB); // false
console.log(new TestB() instanceof Vector); // true
这里的关键是由于Vector
返回的对象与new
创建的对象不同,因此它的类型错误。关于构造函数的一个鲜为人知的事情是,如果它们返回非null
对象引用,则结果new Constructor
是构造函数返回的对象,而不是new
创建的对象。
这是针对那些浏览器支持的代码段class
:
function Vector() {
var v = Object.create(Vector.prototype);
return v;
}
class TestB extends Vector {
constructor() {
super();
}
}
console.log(new TestB() instanceof TestB); // false
console.log(new TestB() instanceof Vector); // true
...并为那些浏览器没有的人提供Babel REPL上的实时副本。
令我惊讶的是,Babel和Chrome都允许我同时使用class Vector
并从中返回值constructor
;我还没有从规范中弄清楚它是否真的有效:
class Vector {
constructor() {
var v = Object.create(Vector.prototype);
return v;
}
}
class TestB extends Vector {
constructor() {
super();
}
}
console.log(new TestB() instanceof TestB); // false
console.log(new TestB() instanceof Vector); // true
为了解决这个问题,您可能需要使用针对每个实例的hack,例如将所有TestB.prototype
方法都复制到实例上。理想情况下,而不是黑客,请尝试Vector
通过聚合(也称为“组合”,例如,通过将Vector
实例作为类的实例的属性)来使用继承,因为它不是为继承而设置的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句