Trivia you should know
- There’s one for every function
prototype
(stereotype) property, the value of which is a normal object - Each object has one
__proto__
The (implicit stereotype) property that points to the (stereotype) of its constructorprototype
), which “inherits” properties from the stereotype - A Function is also an object, and all functions come from the constructor Function
- All objects can be traced back to Object from the prototype chain, and Object is also a constructor
- There’s one for every prototype
constructor
Property that points to the constructor of the association.
Small concept of prototype chain
When accessing a property of an object, it first looks at the property of the object itself. If it is not found, it looks at its implicit __proto__ prototype, that is, its constructor’s __proto__. If it is not found, it looks at its constructor’s __proto__. This leads to a chain structure called a prototype chain
Example 1: Prototype chains
// The constructor
function Parent(name,age){
this.name = name
this.age = age
}
var child = new Parent('Joe'.30)
console.log(child)
console.log(child.__proto__)
console.log(Parent.prototype)
console.log(child.__proto__ === Parent.prototype) //true
// Print the data display
Copy the code
Parent (); Parent (); Parent (); Parent (); Parent (); If the Parent function goes further down the prototype chain, it finds the Object() function, as shown below
Parent.prototype.height = '180cm'
// This prints 180cm, which is the value of the property that child found through the prototype chain
console.log(child.height) // 180cm
Copy the code
The prototype chain for creating the Child object can be shown in the figure below:
What evil does the new object do?
- Create (or construct) an entirely new object
- This brand new object is executed with the [[prototype]] connection: the current object’s __proto__ refers to the constructor’s prototype
- The new object is bound to the this function call
- If the function returns no other object, then the function in the new expression automatically returns a new object
function Foo(name){
this.name = name
}
var foo = new Foo('Ming')
// Parse the new execution step
//1, create a new object
var obj = {}
//2, [[prototype]] connection
obj.__proto__ = Foo.prototype
//3, this refers to the transformation
var foo = Foo.call(obj, 'Ming');
// Return an object (if the constructor returns something else)
return foo
Copy the code
Function and Object
// You are in me, and YOU are in me
console.log(Function instanceof Object) //true
console.log(Object instanceof Function) //true
Copy the code
1. Function (),Object (), prototype (), Function ()
// Prototype object for Function
console.log(Function.prototype);
// The prototype of Object
console.log(Object.prototype);
// Prototype object of the prototype object of Function
console.log(Function.prototype.__proto__);
console.log(Object.prototype === Function.prototype.__proto__);
Copy the code
Complex relationship between Function and Object.
In summary: It can be said that Function itself produces objects, functions, and itself…. __proto__=== function. prototype = function.__proto__ === function. prototype = function.__proto__ == function.prototype This causes object. prototype to become an unwanted wild child, so null