On the JavaScript prototype chain

In JavaScript prototype and prototype chain is a wonderful thing, for the most part is the hardest to understand, to grasp the essence of the prototype and the prototype chain is an important part of advanced JavaScript.
First, the object classification
we believe that any values or variables in JavaScript are objects, but I still need JavaScript objects in divided into the following classes.
First object is the top citizen, he is the parent of all classes, this should be no doubt, because all objects are directly or indirectly derived through it.
function is a first-class citizens, the following explanation will do.
A few, like String, Array, Date, Number, Boolean, Math and other built-in objects are second-class citizens.
The rest were all low-level citizens.

Second, the prototype prototype
first prototype is a property, but it is itself an object.
So that is a prototype of this property does, in fact, all of the JavaScript functions have a prototype of this property, prototype object is itself a function, a function he died right in turn has.
The first I was not verified, mainly to see the second.
We all know that object, Array, Date has a prototype, they function?
They are also a function of, why do you say?
When we define an object or array, you can do is not new new Object = O var (),
A = the Array new new ().
Java learned people know is a new method to instantiate an object, but we all know the real concept of class does not exist in JavaScript, it can only be used to simulate the function, since it can also verify the above approach object and array is a special function.
In fact, the above mentioned several class citizens is a kind of basic functions, in addition to Math tool objects, you should not have seen this new Math () The wording of it!
Of course, such an approach is also being given, so the visit Math. prototype returned is undefined.

Three, __ proto__ and prototype chain

__proto__ is a pointer, it refers to the prototype construction of its object objects, sounds and some hard to pronounce. To give an example!
Here Insert Picture Description
As the above code, is an example of Object o, o so that the pointer to the prototype Object __proto__ configuration of o. This significance is o Object.prototype which method can be used, for example, toString (), o when accessing toString method, first look at itself and if it is not found it will look Object.prototype above in __proto__.
It can be said almost all of the objects have javascript __proto__ include such a pointer Object, let's take a look.

Here Insert Picture Description
In fact, a = 1 is equivalent to a = new Number (1). You can see all of the built-in objects and Object of __proto__ point is an anonymous function, it can be considered an example of them actually function, so before would say function is first-class citizens.
  So the prototype chain in the end what is it? We launched a .__ proto__ is Number.prototype object that is not toString () method, but it is for a fact you can call the toString method, which is the role of the prototype chain. Look at the code below
  Here Insert Picture Description
  to see the results and the above code, we have access to will arrive along __proto__ a prototype Object, that is a fact, is the final call to toString method toString method of accessing Object.prototype. Then a prototype chain is composed of Number.prototype and Object.prototype, when accessing a method or a property, it will first look at itself, and then look for along the prototype chain, find the call, did not find an error. To prove this point, we Number.prototype the above plus a toString method.
  Here Insert Picture Description
  We can see a call is toString method of Number.prototype, after finding stops. In javascript, the end of the prototype chain of almost all objects are Object.prototype

to sum up

__proto__ prototype chain is the key, and the prototype is composed of the prototype chain. Finally, attach a slightly more complex relationship diagram prototype chain and constructors, who are interested can look at.
  
Here Insert Picture Description

Published 74 original articles · won praise 27 · views 9489

Guess you like

Origin blog.csdn.net/qq_42526440/article/details/101100564