admin管理员组文章数量:1279188
Here's some question about oop in js (questions in the code below).
<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>
Here's some question about oop in js (questions in the code below).
<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>
Share
Improve this question
asked Sep 22, 2010 at 15:06
plehouxplehoux
7952 gold badges8 silver badges21 bronze badges
2
- I guess I should clarify my question : What are the drawback of multi-inheritance in js? And why variable a is in global scope? – plehoux Commented Sep 22, 2010 at 15:10
- 2 Then put that in the question - use the edit button – Yi Jiang Commented Sep 22, 2010 at 15:14
3 Answers
Reset to default 7C.prototype = new A();
C.prototype = new B();
Multiple inheritence isn't supported in javascript. All you've done is make C inherit from B instead of A.
You can't. When you do this
C.prototype = new A();
C.prototype = new B();
You are merely changing the object that prototype
points to. So C used to inherit from A, but now it inherits from B.
You can fake multiple inheritance
C.prototype = new A();
for (var i in B.prototype)
if (B.prototype.hasOwnProperty(i))
C.prototype[i] = B.prototype[i];
Now you'd have the properties/methods from both A and B, but you don't really have inheritance, since any changes to the prototype
object of B will not propagate to C.
You need to declare the variable a
with the var
statement in order to make it local to the function.
function A(){
var a = 'a - private FROM A()';
this.a = 'a - public FROM A()';
this.get_a = function(){
return a;
};
}
本文标签: oopMultiple inheritance in javascriptStack Overflow
版权声明:本文标题:oop - Multiple inheritance in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741233856a2362615.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论