Article directory
One click three consecutive
If you find it useful, please like, follow and collect it. Your likes are the motivation for me to update!
Constructors and prototypes
Prior to ES6, objects were not created based on classes, but instead a special function called a constructor was used to define objects and their characteristics.
The constructor has the problem of wasting memory. Every time an object is created, its function will be recreated, which wastes memory space. Therefore, prototypes are introduced to solve the problem of memory waste.
Prototype (especially important, must ask)
The constructor points to the prototype property prototype
The functions assigned by the constructor through the prototype are shared by all objects .
JavaScript stipulates that every constructor has a prototype attribute that points to another object . The object pointed by this prototype is the prototype , and all properties and methods of this object will be owned by the constructor.
We can define those unchanged methods directly on the object (prototype) pointed to by prototype, so that all instances of the object can share these methods, so the prototype is used to store the public methods of the constructor.
What is a prototype?
It is the object pointed to by the constructor's attribute prototype. This object is called the prototype.
What is the role of a prototype?
Shared methods allow constructor methods to be shared by objects.
Object points to a property of the prototype
What must be made clear here is that the object pointed to is called a prototype . Both of these are just attributes pointing to it, so just remember it in English. The prototype refers to this object .
The prototype points to the property constructor of the constructor
The relationship between constructors, instances, and prototypes
Prototype chain (important)
js member search mechanism
It is also the role of the prototype chain
The prototype object this points to
This in the constructor points to our instance object.
The prototype object contains methods, and this in this method points to the caller of this method , which is the instance object .
Functions and closures
This within the function points to
Change this inside the function to point to
call, apply, and bind. These three need to be looked at carefully.
JS strict mode
Closures (super important)
A closure is a function that has access to variables in the scope of another function . ----- JavaScript Advanced Programming A simple understanding is that a scope can access local variables within another function .
The role of closure
Closure effect: Extend the scope of the variable.
Examples of closures: