JavaScript prototype and prototype chain understanding

Briefly describe the understanding of prototype and prototype chain, welcome to study and discuss.

Constructor

function A(){
    
    }

Prototype constructors have prototype prototype properties

A.prototype.name = 'wjx'

The constructor property of the prototype

console.log(A.prototype.constructor)

instance object

let B = new A()

Instance objects have a __proto__ attribute, which points to the prototype of the constructor

console.log(B.__proto__)

several relationship

// 原型与实例对象关系
console.log(A.prototype)//{ name: 'wjx' }
console.log(B.__proto__); //{ name: 'wjx' }
console.log(B.__proto__ === A.prototype);//true
// 原型与构造函数关系
console.log(A.prototype.constructor===A)//true 原型的constructor又指向构造函数
//=============================================================//
// 实例对象与原型的关系
B.name ='wjx123'
console.log(B.name);//wjx123  这里访问到了自己本身的属性
console.log(B.__proto__)//{ name: 'wjx' },他的__proto__指向原型A.prototype.name = 'wjx'
delete B.name //这里在闪吊后,自己的属性就没有了,会访问__proto__
console.log(B.name)//wjx  __proto__指向构造函数A的指向原型,

Summarize:

The instance object first accesses its own attributes, if not, access __proto__,
__proto__ points to the prototype, look for it in the prototype, if you can’t find it, look for it in the prototype of the prototype, what is the prototype of the prototype? object? . The prototype object is generated by the constructor Object()

console.log(Object.prototype.__proto__ === null) // true

So Object.prototype.protothe value of is null and Object.prototype has no prototype, which actually expresses a meaning.
// So when searching for properties, if you find Object.prototype, you can stop searching. ~~~~~~~~~~~~~~~~ This is the prototype chain. . . . . . . . . Personal learning comprehension. If there is something wrong, please correct me.

Learning and exchange button 732278669

Guess you like

Origin blog.csdn.net/weixin_44000173/article/details/124461709
Recommended