In-depth analysis of the relationship between prototype and proto in JavaScript _ javascript skills

Source: Internet
Author: User
Prototype: each function object has a displayed prototype attribute, and each proto object has an internal hidden attribute named _ proto. This article introduces the relationship between prototype and proto in JavaScript. For more information about prototype, see prototype. Each Function object has a displayed prototype attribute, which represents the prototype of the object (Function. the prototype function object is an exception and does not have the prototype attribute ).

_ Proto __: each object has an internal hidden attribute named _ proto _, pointing to its prototype object (chrome and firefox are named _ proto __, and can be accessed ). The prototype chain is formed based on _ proto _.

(Note: The prototype is not based on the function object attribute ).

To put it simply, __proto _ is an internal prototype, and prototype is a constructor prototype (the constructor is actually a function)

The prototype of the constructor is an object.

So what is the 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 constructor is actually a function. The following question is: how can we create an object through this constructor?

Answer: new

The constructor constructs an object.

1. All Constructors/functions _ proto _ point 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

It indicates that Number and so on are all constructors, which are actually an object of Function. That is, it is equivalent to var Number = new Function ();

There are 12 built-in (build-in) Constructors/objects in JavaScript (JSON is added in ES5). The eight constructors that can be accessed are listed here. For example, Global cannot be accessed directly. Arguments is only created by the JS engine during function calling. Math and JSON exist as objects without the need for 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, and even include the root constructor Object and Function itself. All constructors inherit the attributes and methods of Function. prototype. Such as length, call, apply, bind (ES5 ).

Function. prototype is also the only prototype with typeof XXX. prototype being "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 is also mentioned that it is an empty Function. Let's take a look at alert (Function. prototype.

I know that all the Constructors (including built-in and custom) _ proto _ are Function. prototype. Who is the Function. prototype _ proto?

I believe I have heard that functions in JavaScript are also first-class citizens. How can they be reflected? As follows:

Console. log (Function. prototype. _ proto _ =
Object. prototype)
// True

This indicates that all constructors are also common JS objects, and you can add/delete attributes to the constructor. It also inherits all methods on Object. prototype: toString, valueOf, hasOwnProperty, and so on.

Who is the last Object. prototype _ proto?

Object. prototype. _ proto _ =
Null //
True

The following is a diagram of the Function, Object, Prototype, and _ proto _ memory relationships.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.