为什么这种继承类型在JavaScript中不起作用。有什么方法可以执行此步骤。
请帮忙
function base() {}
base.prototype.findFirst = function() {
console.log("FindMe First");
}
function base2() {}
base2.prototype.findMe = function() {
console.log("FindMe ");
}
function inherit() {
base.call(this);
base2.call(this);
}
inherit.prototype = base.prototype;
inherit.prototype.constructor = inherit;
inherit.prototype = base2.prototype;
inherit.prototype.constructor = inherit;
var test = inherit();
test.findFirst();
test.findMe();
最佳答案
您将使用base.prototype覆盖原型,然后使用base2.prototype覆盖原型。因此它存储了base2.prtotype,即第二个。现在,如果您创建继承类的实例var test = new inherit();
,您会看到测试具有base2.property,即它在fimeMe()
中具有test.property.findMe();
方法。
为了实现您的目标,您应该尝试扩展或参考
Mixin
Multiple Inheritance