为什么这种类型的继承在 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覆盖原型(prototype),然后使用base2.prototype覆盖原型(prototype)。所以它存储了base2.prtotype,即第二个分配的。现在,如果您创建继承类的实例
var test = new继承();
你会看到test有base2.property,即它在test.property.findMe();中有
。
为了实现你的目标,你应该尝试扩展或引用
Mixin
Multiple Inheritance fimeMe()
方法。
关于javascript - JavaScript 中的多重继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25974977/