Sta*_*tec 3 javascript inheritance
该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
Run Code Online (Sandbox Code Playgroud)
在这里,我认为c
从多个类继承.这怎么不是多重继承?
多重继承是指父级在层次结构中处于同一级别时:
c ---> b ---> Object.prototype ---> null
\---> a ---> Object.prototype ---> null
Run Code Online (Sandbox Code Playgroud)
在这种情况下,它是从b
继承自其他类的类继承的简单a
:
c ---> b ---> a ---> Object.prototype ---> null
Run Code Online (Sandbox Code Playgroud)
附录:虽然效果可能看起来相似(b
通过原型链中的查找也会在"c"中找到属性),但请注意多重继承允许的区别a
和b
具有完全不同的继承链(实际上,继承"树") "),这在你的例子中显然不是这样.