In javascript, _ proto _ and prototype _ basic knowledge-js tutorial

Source: Internet
Author: User
This article describes in detail the internal prototype _ proto _ of javascript and the prototype of the constructor, as well as the similarities and differences between them. It is very detailed. If you need it, come and study it. _ 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 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)

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

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:

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:

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, 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:

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 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:

The Code is 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?

The Code is as follows:


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

It has reached the top, and is null.

Do you know the difference between _ proto _ and prototype in javascript? If you have any questions, leave a message. Let's discuss it together.

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.