gpt4 book ai didi

javascript - JavaScript 中的多重继承

转载 作者:行者123 更新时间:2023-11-28 19:35:34 26 4
gpt4 key购买 nike

为什么这种类型的继承在 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();中有fimeMe()方法。 。为了实现你的目标,你应该尝试扩展或引用 Mixin Multiple Inheritance

关于javascript - JavaScript 中的多重继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25974977/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com