当前位置:网站首页>XXXI` Prototype ` displays prototype properties and`__ proto__` Implicit prototype properties

XXXI` Prototype ` displays prototype properties and`__ proto__` Implicit prototype properties

2022-04-23 20:22:00 Isn't this more popular than Bo people?

This belongs to js Knowledge in .

1. js Several ways to create objects in (4 Kind of ) The way :

So let's see js Several ways to create objects in (4 Kind of ) The way :

  • adopt Object() Method to create
  • Create objects literally
  • Create objects through custom functions
  • Create objects through factory mode

Reference resources :https://blog.csdn.net/dearzhuoer/article/details/84574140
Let's take a look at the third way, and you'll know —— Create... Through functions !!!
What do you mean ? Functions are objects ? Pretty good !!!

2. Functions are objects ?

  • stay js The object is the existence of God , Everything is the object ( Include function ), That's how you understand .
  • Every function we create , The parser will add an attribute to the function prototype( Except for some built-in functions ). This property is a pointer , Points to a prototype object .( This attribute can also correspond to a prototype object ——prototype: Prototype object
  • If this function is a normal function , Then this prototype Attributes have no effect ; But if this function is used as a constructor to create an object , Then this object will have an implicit attribute __proto__, This implied attribute __proto__ Also point to the prototype object .
    So we have the following conclusions : Constructor name .prototype= Object name .__proto__

Reference resources :https://blog.csdn.net/dong001687/article/details/81836575

3. prototype and __proto__

prototype: Show prototype properties
__proto__: Implicit prototype properties
As for why a display , An implicit , After reading the second point above, we also know .prototype so ,__proto__ invisible .

4. give an example :

function Mytest(name,age){
    
    this.name = name;
    this.age = age;
}

var xiaoming = new Mytest(' Xiao Ming ',18) 
console.log(Mytest.prototype) //{constructor: ƒ}
console.log(xiaoming.__proto__) //undefined
console.log(Mytest.prototype==xiaoming.__proto__) //true

版权声明
本文为[Isn't this more popular than Bo people?]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204232021100345.html