The relationship between prototype and _ proto _ in Javascript is described in detail. prototype _ proto _

Source: Internet
Author: User

The relationship between prototype and _ proto _ in Javascript is described in detail. prototype _ proto _

Preface

When I learned the prototype, I felt my head was too big )/~~ In particular, prototype and _ proto _ are too stupid to find out more information. Based on your understanding, the following is a summary:

1. constructor:

Constructor: The new Keyword can be used to create functions for specific types of objects. For example, objects and arrays are built-in native constructor, which automatically appears in the execution environment during runtime and can be directly used. As follows:

Var arr = new Array (); // use the Array constructor to create an array instance arrarr [0] = "a"; arr [1] = "B "; alert (arr); // a, bvar obj = new Object (); // creates an Object instance objobj using the Object constructor. name = "c"; obj. age = 12; alert (obj. name); // c

We can create constructors and customize attributes and methods for them, such:

// Create the constructor Personfunction Person (name, age) {this. name = name; this. age = age; this. sayName = function () {alert (this. name) //};} // use the new keyword to generate the Person instance var person1 = new Person ("Tom", 22); var person2 = new Person ("Jerry ", 21); person1.sayName (); // Tomperson2.sayName (); // Jerry

Note the following:

  • The name of the constructor must always start with an uppercase letter (mainly to distinguish it from a non-constructor, that is, it is different from a common function)
  • A constructor is a function. The syntax for defining constructor is the same as that for defining common functions. The difference between constructors and common functions is that they are used in different ways. Any function can be called as a constructor by using the new operator. A common function is called without the new operator.
Function Person (name, age) {this. name = name; this. age = age; this. sayName = function () {alert (this. name) //};} // as the constructor, use var person = new Person ("Tom", 22); person. sayName (); // Tom // use Person ("Jerry", 30) as a common function; // Add to windowsayName (); // Jerry is equivalent to window. sayName ();

Ii. Prototype objects:

Each function has a prototype attribute, which is a pointer to the prototype object. The prototype object is created when the function is defined. The purpose of the prototype object is to include the attributes and Methods shared by all instances.

Function Person () {}// attributes and methods of the custom prototype object Person. prototype. name = "Tom"; Person. prototype. age = 25; Person. prototype. sayName = function () {alert (this. name) ;}; // all attributes and methods in the prototype object will be automatically shared by all instances var person1 = new Person (); var person2 = new Person (); person1.sayName (); // Tomperson2.sayName (); // Tom

Once a new function is created, each function obtains a prototype attribute after it is created. This attribute points to the prototype object of the function (the prototype object is created simultaneously when the function is defined ), this prototype object has another attribute named "constructor", which in turn points to the function itself to achieve a loop pointing,

For example, in the above example:Alert (Person. prototype. constructor = Person); // true is returned.

function Person(){}alert(Person.prototype.constructor===Person);//true

3. _ proto _ (note that there are two "_" on both sides of proto "_")

After a constructor is called to create a new instance, the instance contains a pointer [[Prototype], pointing to the Prototype of the constructor, there is no standard method in the script to access [Prototype], but most browsers support access through _ proto.

Function Person () {}// attributes and methods of the custom prototype object Person. prototype. name = "Tom"; Person. prototype. age = 25; Person. prototype. sayName = function () {alert (this. name) ;}; // all attributes and methods in the prototype object will be automatically shared by all instances var person1 = new Person (); var person2 = new Person (); person1.sayName (); // Tomperson2.sayName (); // Tomalert (person1. _ proto __= = Person. prototype); // true

The preceding sample code is used as an example. The relationship between objects is shown in:

 

Summary:

① As long as a function is created, the prototype object of the function is also created, and the attributes and methods in the prototype object are shared by the instances created by the corresponding constructor.

② Each function will obtain a prototype attribute after it is created. This attribute points to the prototype object of the function.

③ The _ proto _ attribute of each object points to the prototype of its constructor.

Well, the above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.

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.