Front-end stereotypes - JS advanced learning

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.
Insert image description here

Insert image description here
Insert image description here
Insert image description here

Insert image description here
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

Insert image description here

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

Insert image description here

The relationship between constructors, instances, and prototypes

Insert image description here

Prototype chain (important)

Insert image description here

js member search mechanism

It is also the role of the prototype chain
Insert image description here

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

Insert image description here
Change this inside the function to point to
call, apply, and bind. These three need to be looked at carefully.
Insert image description here

JS strict mode

strict mode
Insert image description here

Closures (super important)

Insert image description here

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 .

Insert image description here
Insert image description here

The role of closure

Closure effect: Extend the scope of the variable.
Insert image description here
Examples of closures:

Insert image description here

recursion

Insert image description here

shallow copy deep copy

Insert image description here

Guess you like

Origin blog.csdn.net/Tommy__li/article/details/129382525