__proto__ is the internal prototype, prototype is the constructor prototype (the constructor is actually a function)
The prototype of the constructor is an object
So what is a constructor?
If you want to create an object, you must first have an object constructor, just like in PHP. If you want to create an object, you must first have a class
The essence of a constructor is a function. The following question is: How to create an object through this constructor?
Answer: new
The constructor constructs an object.
1. The __proto__ of all constructors/functions points to Function.prototype, which is an empty function (Empty function)
explains that Number and so on are all constructors, and these constructors are actually an object of Function. In other words, it is equivalent to var Number = new Function();
There are a total of 12 built-in constructors/objects in JavaScript (JSON is newly added in ES5). Here are 8 accessible constructors. The rest such as Global cannot be accessed directly, Arguments are only created by the JS engine when the function is called, Math and JSON exist in the form of objects and do not require new. Their __proto__ is Object.prototype. As follows
The "all constructors/functions" mentioned above certainly include custom ones. As follows
What does this mean?
All constructors come from Function.prototype, even the root constructor Object and Function itself. All constructors inherit the properties and methods of Function.prototype. Such as length, call, apply, bind (ES5).
Function.prototype is also the only prototype whose typeof XXX.prototype is "function". The prototype of other constructors is an object. As follows
Oh, it was also mentioned above that it is an empty function, let’s take a look at alert(Function.prototype).
We know that the __proto__ of all constructors (including built-in and custom) are Function.prototype, so who is the __proto__ of Function.prototype?
I believe you have heard that functions in JavaScript are also first-class citizens, so where can you show this? As follows
This shows that all constructors are also ordinary JS objects, and attributes can be added/removed to the constructor. At the same time, it also inherits all methods on Object.prototype: toString, valueOf, hasOwnProperty, etc.
Who is the __proto__ of Object.prototype?
It has reached the top and is null.
Do you have some understanding of the difference between __proto__ and prototype in javascript? If you have any questions, please leave a message and let’s discuss it together