Prototype of JavaScript I know

Source: Internet
Author: User
I. Prototype concept and purpose prototype literal translation is a prototype and is the main means for JavaScript to implement inheritance. Prototype is a reserved property of functions in Javascript, and its value is an object (we can call this object "prototype object "), objects constructed by using this function as constructor automatically have the member attributes and methods of the prototype object of the constructor. The key points are: 1. prototype is a required attribute of a function (the written description is "Reserved attribute") (as long as it is a function, there must be a prototype attribute) 2. the value of prototype is an object 3. you can modify the value of the prototype attribute of a function at will. 4. an object automatically owns the prototype member attributes and methods of the constructor of this object. Example code: // defines a function (constructor ), and define some attributes and methods to inherit the object constructed by another constructor.
VaR function1 = function (){
This. Name = "function1 ";
This. saysomething = function () {alert ("This's a method of" + this. Name);} // defines a method

// Define another constructor
VaR function2 = function (){


Function2.prototype = new function1 (); // sets the prototype attribute of the constructor function2 to an object constructed by function1, so that objects constructed by function2 (and originally objects without any attributes and methods) have the attributes and methods of function1.
VaR obj1 = new function1 ();
Obj1.saysomething (); // No member of obj1. The prototype mechanism is used to enjoy the attributes and methods of the function1 object. Of course, the above example is far from the "inheritance" actually used, but it also shows the meaning of inheritance: an object has the attributes and methods of another object. (For example, if a son has his father's blood type and temper, humans inherit the animal's instinct, such as eating and fighting)


Ii. What happened behind prototype? The concepts and functions of prototype are outlined in the above section, but it is not enough to understand prototype. Now let's take a look at how prototype works: Let's take a look at the process of creating an object in the new form: var OBJ = new func (); // use func () the process of creating an object OBJ as a constructor is like this: After the JavaScript engine first encounters the keyword new, it immediately opens up a piece of memory, create an empty object (and point this to this object), and then execute the constructor func () construct this empty object (all the attributes and methods in the constructor assemble this blank object one by one, that is why the constructor is called a constructor). In fact, another thing between new and the execution constructor is that the engine did not explicitly tell us, but secretly did it. This is to assign the prototype object to this empty object. We have to mention that something that is retained by the system like prototype and equally important: __proto ____ PROTO _ is a built-in property automatically owned by an object (note: prototype is the built-in property of the function, __proto _ is the built-in property of the object, but they all point to the same object, that is, the object to be inherited ), chrome and FF can be used to access the _ PROTO _ attribute of an object. ie cannot. It is precisely because the _ PROTO _ of an object points to the prototype object of the constructor of this object that makes this object understand the prototype object of its constructor, and has the attributes and methods of this prototype object. Therefore, the VaR OBJ = new func () process is more specific: 1. when the Javascript parsing engine encounters new, it opens up a piece of memory and creates an empty object, and points "this" to this empty object 2. the javascript parsing engine points the _ PROTO _ of this empty object to the default prototype object of the constructor that follows. (After pointing to the prototype object, the parsing engine will know "Oh, this object must have the attributes and methods of this prototype object. ") 3. the javascript parsing engine executes the code in the constructor body and formally begins the process of constructing (or assembling) This empty object. name = "XXX", this. sayhello = function (){...} etc.) 4. the object is constructed and assembled, and assigned to the variable on the left of the equal sign. 3: I encountered a problem when trying to compare the advantages and disadvantages of using call and prototype to implement inheritance in JS, and asked for help on the csdn forum, if you are interested, you can click here to view details. Here, I would like to thank liangws for their answers. As mentioned in the topic of this Article, these are all "javascript I know". Due to my limited level, the descriptions in this article are definitely incorrect, so they are for reference only, if you find any errors, please click it.
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: 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.