As already mentioned in the previous section, each functional function has a prototype, which is a prototype. Add one more word here- each object has a __proto__that can become an implicit prototype.
This __proto__ is a hidden property, JavaScript does not want developers to use this property value, and some low-version browsers do not even support this property value. So you don't have __proto__ smart tips in a very advanced, smart editor like Visual Studio 2012, but you don't have to worry about it, just write it out.
Above, obj.__proto__ and Object.prototype have the same properties! So coincidence!
The answer is the same.
obj is essentially created by the object function, so obj.__proto__=== object.prototype. We can use a graph to represent.
That is, each object has a __proto__ property that pointsto the prototype of the function that created the object.
So the "object prototype" is also an object, where does its __proto__ point?
Good question!
Before explaining "Object prototype", let's talk about the prototype of the custom function. The prototype of a custom function is essentially the same as var obj = {}, which is created by object, so its __proto__ points to object.prototype.
But Object.prototype really is a special case-its __proto__ point is null, remember to remember!
And--the function is also a kind of object, function also has __proto__?
Another good question! --Of course there is.
Functions are also not popped out of a stone seam, and functions are created. Who created the function? --function--pay attention to this uppercase "F".
And look at the following code.
In the above code, the first method is to compare the traditional method of function creation, and the second is to create it with new Functoin.
The second way is not recommended at all .
This is just to show that functions are created by function.
Well, according to the words above--the object's __proto__ points to the prototype of the function that created it, it will appear: object.__proto__ = = = Function.prototype. represented by a graph.
, it is obvious that the custom function foo.__proto__ point to function.prototype,object.__proto__ point to Function.prototype, alas, how do you have a ... function.__proto__ point to Function.prototype? This is not a circular reference?
Right! is a circular structure.
In fact, just a little bit to understand. function is also a function, which is an object and has a __proto__ property. Since it is a function, it must be created by functions. So--function is created by itself. So its __proto__ points to its own prototype.
It's a lot of space, and I guess it's annoying. It's almost over.
Last question: Function.prototype points to the object, does its __proto__ also point to Object.prototype?
The answer is yes. Because the object that Function.prototype points to is also a normal object created by object, it follows the basic rules.
OK This section ends, isn't it messy?
Chaos is normal. Let it be a mess in this section, and next we'll ask another old friend to help you figure it out. This old friend is--instanceof.
For details, see below section decomposition.
---------------------------------------------------------------------------
This article has been updated to the directory of Understanding JavaScript prototypes and scope series, and more on understanding JavaScript prototypes and Scope series.
Also, please pay attention to my Weibo.
Also welcome to follow my other tutorials-don't be afraid, are free!
"Microsoft petshop4.0 Source Interpretation Video" "Json2.js Source Interpretation video"
--------------------------------------------------------------------------
Understanding JavaScript Prototypes and Scope series (4)--Implicit prototypes