Home  >  Article  >  Web Front-end  >  An in-depth analysis of the relationship between prototype and proto in JavaScript_javascript skills

An in-depth analysis of the relationship between prototype and proto in JavaScript_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:32:19976browse

prototype, each function object has a displayed prototype attribute, which represents the prototype of the object (the exception is Function.prototype function object, which has no prototype attribute).

__proto__: Each object has an internal hidden attribute named __proto__, which points to its corresponding prototype object (the name is __proto__ in chrome and firefox, and can be accessed). The prototype chain is formed based on __proto__

(note: not based on the property prototype of the function object).

To put it simply: __proto__ is the internal prototype, and prototype is the constructor prototype (the 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. 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)

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

Math.__proto__ === Object.prototype
// true
JSON.__proto__ === Object.prototype
// true

The "all constructors/functions" mentioned above certainly include custom ones. 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

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

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?

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

Let me share with you a memory relationship diagram of Function, Object, Prototype, and __proto__

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