In the previous two chapters, "All JavaScript is Object 1" and "JavaScript is all object 2", it was mentioned that:
1, "All (reference type) are objects"
2, "Each function has a prototype"
3, "Each object has a __proto__"
So, here's the question, in the essay, "Everything in JavaScript is Object 2," isn't there?
That, according to "Everything (reference type) is the object", the figure of the function FN is not also an object?
Then it also has __proto__, then it is pointing to who?!!
Answer: Function.prototype
Why do you say that?
Because the function FN is not created through functions?
So fn.__proto__ is pointing to Function.prototype.
Don't believe me?
Let's write a demo and run the code to verify it. Open the Chrome debugger below.
<!DOCTYPE HTML> <Head> <title>Ttt</title> <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"/> </Head> <Body> <Script> //Create an FN function functionFn () {}//prints the implicit pointer of FN, which is fn.__proto__Console.log (fn.__proto__); </Script> </Body></HTML>View Code
Holy crap, what the hell is this?!
I just want to say that it should be the browser (my chrome version is 48) does not support Function.prototype bar.
So, how do you prove that what you said above fn.__proto__ point to is Function.prototype?
So, we know that prototype has an attribute constructor, it is pointing to the function itself, so, if fn.__proto__ points to Function.prototype, then fn.__proto__. constructor is the function that points to.
So as long as we verify that fn.__proto__.constructor points to a function, then we can conclude that the fn.__proto__ point is Function.prototype ha.
Demo and chrome are as follows
<!DOCTYPE HTML> <Head> <title>Ttt</title> <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"/> </Head> <Body> <Script> //Create an FN function functionFn () {}/*First call FN's implicit pointer (__proto__), and then go through Fn.__proto__ 's constructor to see what it's pointing to.*/Console.log (Fn.__proto__.constructor); </Script> </Body></HTML>View Code
Haha, from the above can be seen fn.__proto__.constructor really point is function, so this will believe it, fn.__proto__ Point is function.prototype.
So modify the flow chart above (for a clearer structure, we will delete the Fn1 object in, leaving only one FN object):
Gee, look at the transformation of the flowchart, there is a doubt, function is not an object, then its __proto__ point to whom?
The answer: its own prototype.
What's the remark?
Because functions are also a function, it must be created by function, which is itself, so its __proto__ points to function.prototype.
On top, we know that Fn.__proto__.constructor is pointing to the function, so if you want to prove that function.__proto__ is pointing to Function.prototype, just function.__ Proto__.constructor points to a function Ouke, that is, fn.__proto__.constructor.__proto__.constructor points to a function.
The code is as follows:
<!DOCTYPE HTML> <Head> <title>Ttt</title> <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"/> </Head> <Body> <Script> //Create an FN function functionFn () {} console.log (fn.__proto__.constructor.__proto__.constructor); </Script> </Body></HTML>View Code
It seems that function.__proto__ is indeed pointing to the function.prototype.
According to JavaScript everything is Object 2, mentioned prototype is also the object, so Function.prototype also has __proto__, and point to is object.prototype.
From above, we know that fn.__proto__ is pointing to function.prototype, so we check function.prototype.__proto__ is not pointing to the object.prototype, that is to see fn.__ Proto__.__proto__ is not pointing to the object.prototype slightly.
The validation code is as follows:
<!DOCTYPE HTML> <Head> <title>Ttt</title> <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"/> </Head> <Body> <Script> //Create an FN function functionFn () {} console.log (fn.__proto__.__proto__); </Script> </Body></HTML>View Code
Verification Complete: function.prototype.__proto__ is pointing to Object.prototype.
So, once again, the flowchart can be modified:
Hey, did you see object as well? And who is it that __proto__ points to? And who is the __proto__ of its prototype?
Answer: object is also function created, so its __proto__ point is definitely function.prototype. Since Object.prototype is the last chain, the __proto__ of it (Object.prototype) points to null.
Object is a function, its __proto__ points to Function.prototype to understand.
But, you say Object.prototype's __proto__ is pointing to null?!!
Yes, object.prototype.__proto__ points to null, and the code is as follows:
<!DOCTYPE HTML> <Head> <title>Ttt</title> <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"/> </Head> <Body> <Script> //create an object obj varobj= NewObject (); /*obj.__proto__ points to Object.prototype, so obj.__proto__.__proto__ is object.prototype.__proto__ */Console.log (obj.__proto__.__proto__); </Script> </Body></HTML>
View Code
Hehe, that's right.
Re-engineered to get under:
Well, if you can understand the above picture, congratulations, the process of the prototype chain is estimated to be almost understandable.
JavaScript is all about Object 3