1. All objects in js are generated by functions

let obj={a:1.b:2} is equivalent tolet obj=new Object() obj.a=1 obj2.=2
ObjectIs the constructor that generates the objectlet str='123'When the base string needs to call a string object's method or query value (which the base string doesn't have), JavaScript automatically converts the base string to a string object and calls the corresponding method or executes a query. Js internally converts the base string to a string object when calling a string methodlet strcopy=new String('123'String method or propertylet str=strcopy
strcopy=null

Copy the code

2. How do JS prototypes inherit

This is my personal understanding at present

First of all, clear

1. Objects have a __proto__ attribute. This is a hidden attribute that JS does not want developers to use to point to the prototype object that generated the object’s constructor.

  let obj={a:1.b:2} the constructor of the object isObjectSo the obj. __proto__ = = =Object.prototype
  console.log(obj.__proto__===Object.prototype)//true
  
  let str='123'The constructor of a string isStringSo STR. __proto__ = = =String.prototype
  console.log(str.__proto__===String.prototype)//trueNumber and other basic typesStringThe same cannot be said all the timeCopy the code

2. Functions have two attributes (except the arrow function). A prototype function has a __proto__ attribute because it is an object

Function is generated by new Function so Function __proto__ points to function.prototype

Prototype is a prototype Object and therefore has __proto__. The constructor of the Object mentioned above is Object so prototype.proto

Object. Prototype and object.prototype. __proto__ to null

function test(){
   console.log('1222')
}
test.ad='123'Functions can also define properties because functions arenew Function() out of the object so the function also has __proto__ but prototype is the function's unique constructorFunctionSo the constructor prototype of the function isFunction.prototype
console.log(test.__proto__===Function.prototype)//true

Function__proto__ points to its own prototype cosnole.log(Function.__proto__===Function.prototype)



Copy the code

3. When accessing the properties of the object, the user will first look for its own properties and then look for the prototype that generates the object if it does not have such properties

Object. Prototype Returns null for its constructor

New Function(). What is the constructor of the Function

The prototype object for the Function constructor is function.prototype

New Object(); the function.prototype constructor is an Object Function. Proto__ === object. prototype and object.proto. __proto__===null return undefined

Object same as above first find the prototype object that generated the constructor of this object

 function myFun(){
    this.a='123'
 }
 let obj=new myFun()
 obj.__proto__===myFun.prototype
 myFun.prototype.__proto__===Object.prototype
 Object.prototype.__proto__===null
Copy the code

4. Conclusion: Two things to remember

1. All objects have a __proto__ attribute. This attribute refers to the prototype object of the Function that generated the object

2. All functions have two attributes: a prototype function has a __proto__ attribute because it is an object

The prototype legend is shown below