__proto__ is an internal prototype, and prototype is a constructor prototype (constructor is actually a function)
The prototype of the constructor is an object
So what is a constructor?
To create an object, you must first have an object constructor, just like in php. 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)
The code copy is as follows:
Number.__proto__ === Function.prototype
// true
Boolean.__proto__ === Function.prototype
// true
String.__proto__ === Function.prototype
// true
Object.__proto__ === Function.prototype
// true
Function.__proto__ === Function.prototype
// true
Array.__proto__ ===
Function.prototype
// true
RegExp.__proto__ === Function.prototype
// true
Error.__proto__ ===
Function.prototype
// true
Date.__proto__ ===
Function.prototype
// true
This shows that Number and other constructors are actually objects of Function. That is to say, it is equivalent to var Number = new Function();
There are 12 built-in constructors/objects in JavaScript (JSON is newly added in ES5), and here are 8 constructors that can be accessed. The rest is that Global cannot be accessed directly, Arguments are only created by the JS engine when function calls, Math and JSON exist in object form, without new. Their __proto__ is Object.prototype. as follows
The code copy is as follows:
Math.__proto__ === Object.prototype
// true
JSON.__proto__ === Object.prototype
// true
The "all constructors/functions" mentioned above certainly include custom ones. as follows
The code copy is as follows:
// Function declaration
function Person()
{}
// Function expression
var Man
=
function()
{}
console.log(Person.__proto__ === Function.prototype)
// true
console.log(Man.__proto__ ===
Function.prototype)
// true
What does this mean?
All constructors come from Function.prototype, and even include 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 with typeof XXX.prototype as "function". The prototypes of other constructors are all objects. as follows
The code copy is as follows:
console.log(typeof Function.prototype)
// function
console.log(typeof Object.prototype)
// object
console.log(typeof Number.prototype)
// object
console.log(typeof Boolean.prototype)
// object
console.log(typeof String.prototype)
// object
console.log(typeof Array.prototype)
// object
console.log(typeof RegExp.prototype)
// object
console.log(typeof Error.prototype)
// object
console.log(typeof Date.prototype)
// object
console.log(typeof Object.prototype)
// object
Oh, and it is also mentioned above that it is an empty function, take a look at it under alert(Function.prototype).
I know that all constructors (including built-in and custom) are Function.prototype, so who is __proto__ of Function.prototype?
I believe everyone has heard that functions in JavaScript are also first-class citizens, so where can they be reflected? as follows
The code copy is as follows:
console.log(Function.prototype.__proto__ ===
Object.prototype)
// true
This shows that all constructors are also ordinary JS objects, and they can add/delete attributes, etc. to the constructor. At the same time, it also inherits all methods on Object.prototype: toString, valueOf, hasOwnProperty, etc.
Who is __proto__ of Object.prototype?
The code copy is as follows:
Object.prototype.__proto__ ===
null //
true
It's already reached the top, for null.
Have you any understanding of the difference between __proto__ and prototype in javascript? If you have any questions, leave a message and let us discuss it together