This is the 26th day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021.”

preface

Before is busy with business development, the basic knowledge of front end to check what are used, rarely has time to sink down and their induction, summarizes the basis, sometimes, what use, feel worthy of pay attention to the thing, are also recorded in his note, also not timely, and then as time goes by, one record everything is at sixes and sevens, I haven’t done it yet. This time, write the basic series of the front end

I plan to chat in a relaxed, chatty tone. If I’m not good at writing, I’ll let you know

Quickly review

Above,

  1. Deep copy
  2. Shallow copy

The prototype

Did you ever wonder when we created an object let obj = {name: ‘aaa’} and found that we could use many functions that we didn’t define?

When we print obj in the browser, you will notice that there is a __proto__ attribute on obj, so the question is related to this attribute.

In fact, every JS object has a __proto__ attribute, which refers to the prototype. This property is not recommended now, it’s just something that browsers implemented early on to give us access to the internal property [[prototype]].

I’m still not sure what a prototype is, so let’s take a look at what’s inside __proto__.

Ps, if you are in the actual development, debugging, often use the console, you can easily view the properties you want to see.

As you can see from this, a prototype is also an object, and this object contains many functions, so we can conclude that in the case of obj, __proto__ can find a prototype object that defines many functions for us to use.

We can also find a constructor property in the above figure

Opening the constructor property we can see that there is also a Prototype property and that this property corresponds to the same value we saw earlier in __proto__. The constructor property points back to the prototype via the prototype property, but not all functions have this property. Function.prototype.bind() doesn’t have this property.

A prototype chain is a chain of objects connected by __proto__. The reason obj can access valueOf is because OBj found valueOf through the prototype chain.

conclusion

  • ObjectIs the father of all objects. All objects can pass__proto__To find it
  • FunctionIs the father of all functions, all functions can pass__proto__To find it
  • Function of theprototypeIt’s an object
  • The object’s__proto__The property points to the stereotype,__proto__The prototype chain is formed by connecting objects and stereotypes

Refer to the article

  1. In-depth analysis of the various difficulties in the prototype

Ps: Dry words or more boring, and hard to impress, we are still in daily use, more experience