MDN在Javascipt中给出了对继承的解释(注释显示了原型链):
var a = {a: 1};
// a ---> Object.prototype ---> null
var b = Object.create(a);
// b ---> a ---> Object.prototype ---> null
console.log(b.a); // 1 (inherited)
var c = Object.create(b);
// c ---> b ---> a ---> Object.prototype ---> null
var d = Object.create(null);
// d ---> null
console.log(d.hasOwnProperty);
// undefined, because d doesn't inherit from Object.prototype
在我看来,c
继承自多个类。这个不多重继承怎么样?
答案 0 :(得分:8)
多重继承是指父级在层次结构中处于同一级别时:
c ---> b ---> Object.prototype ---> null
\---> a ---> Object.prototype ---> null
在这种情况下,它是继承自其他类b
的类a
的简单继承:
c ---> b ---> a ---> Object.prototype ---> null
附录:虽然效果可能看起来相似(b
的属性也将通过原型链中的查找找到"在c中),请注意多重继承允许a
和b
具有完全不同的继承链(事实上,继承"树"),这在你的例子中显然不是这种情况。
答案 1 :(得分:1)
多重继承意味着从两个或多个类继承并行,即具有两个或更多直接祖先。他们形成一棵树。
在你的情况下,只有一个直接祖先:b,a是b的直接祖先,但间接是c。它们形成一条直线链。