Home >Web Front-end >JS Tutorial >Detailed explanation of __proto__ and prototype in javascript_Basic knowledge

Detailed explanation of __proto__ and prototype in javascript_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 16:30:291265browse

__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)

Copy code The code 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

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

Copy code The code 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

Copy code The code 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, 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

Copy code The code 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, 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

Copy code The code is as follows:

console.log(Function.prototype.__proto__ ===
Object.prototype)
// true

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?

Copy code The code is as follows:

Object.prototype.__proto__ ===
null //
true

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn