Inheritance and prototype chain (a) prototype-based inheritance chain

 

When it comes to inheritance, JavaScript is only one structure. Each instance of an object has a private attribute _proto_ prototype object it points to the constructor (prototype), the prototype object has its own prototype object (__proto__), up until the layer of the prototype for an object  null. By definition, null no prototype, and as the prototype chain last link in.

 

function f(){
    this.a = 1;
    this.b = 2;
}

let o = new f();
f.prototype.b = 3;
f.prototype.c = 4;

console.log(o.a); //1
console.log(o.b); //2
console.log(o.c); //4
console.log(o.d); //undefined;

For more details, see the official website: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Inheritance_and_the_prototype_chain

 

In the recent review of official documents, to the basics of a solid, stable foundation for a hit.

Published 98 original articles · won praise 4 · views 20000 +

Guess you like

Origin blog.csdn.net/weixin_42416812/article/details/100098561