javascript - JavaScript 中的多重继承

标签 javascript oop multiple-inheritance

这里有一些关于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/

相关文章:

javascript - 对默认变量使用解构赋值和其余语法

javascript - 如何在单击下一个隐藏上一个10并显示新的jquery后拆分所有元素li以显示10

javascript - 使用自定义排序函数在 JavaScript 中对多维数组进行排序

c++ - 以错误的方式在子类中调用函数的最佳方法是什么?

带有接口(interface)的 Java 编译错误

javascript - 如何在 MathQuill 0.10 中同步文本字段和公式字段内容?

Java - 与对象分配混淆

java - 类和具体类

具有多重继承的 C++ 链表模板?

java - 派生类中的变量可能尚未初始化(使用构造函数)