gpt4 book ai didi

javascript - JavaScript 中的多重继承

转载 作者:行者123 更新时间:2023-11-28 11:06:11 27 4
gpt4 key购买 nike

这里有一些关于js中oop的问题(问题在下面的代码中)。

<html>
<script>
function A(){
a = 'a - private FROM A()';
this.a = 'a - public FROM A()';
this.get_a = function(){
return a;
}
}

function B(){
this.b = 'b - private FROM B()';
this.a = 'a - public FROM B() ';
}

C.prototype = new A();
C.prototype = new B();
C.prototype.constructor = C;
function C() {
A.call(this);
B.call(this);
}

var c = new C();

//I've read paper about oop in Javacscript but they never talk
//(the ones have read of course) about multiple inheritance, any
//links to such a paper?

alert(c.a);
alert(c.b);
alert(c.get_a());

//but

//Why the hell is variable a from A() now in the Global object?
//Look like C.prototype = new A(); is causing it.

alert(a);

</script>
</html>

最佳答案

C.prototype = new A();
C.prototype = new B();

JavaScript 不支持多重继承。您所做的只是让 C 继承 B 而不是 A。

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

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