Basic of JavaScript object-oriented technology (iv) -- Transition

Source: Internet
Author: User

Transferred from:Http://developer.51cto.com/art/201105/261407.htm

 

This article is written in chapter 7, 8 and 9 of <JavaScript-the definitive guide, 5th edition>, I will try to describe the Javascript object-oriented technology (Object/array-> function --> class/constructor/prototype) according to the structure of the original book ). I will attach the original English statement for your reference.

Class, constructor, prototype

Note: As mentioned above, each function contains a prototype attribute, which points to a prototype object. Do not confuse it.

Constructor:

The new operator is used to generate a new object. New must be followed by a function, that is, the constructor we often call. What is the working principle of the constructor?
Let's take a look at an example:

JS Code

 
 
  1. function Person(name,sex) {  
  2. this.name = name;  
  3. this.sex = sex;  
  4. }  
  5. var per = new Person("sdcyst","male");  
  6. alert("name:"+per.name+"_sex:"+per.sex); //name:sdcyst_sex:male  

The following describes the procedure:

A function (not a method, but a common function) is created. Note that this keyword is used. previously we mentioned this keyword to indicate the object that calls this method, that is, when you call a "method" through an object, this keyword will point to this object (this points to the entire script domain, or the domain of the function if the function is called directly without an object, which we will not discuss in detail here ).

When we use the new operator, JavaScript will first create an empty object, and then this object is referenced by the this keyword of the method (function) next to New! Then, through this operation in the method, the newly created object is Assigned attributes. finally, the processed object is returned. in this case, the above example is clear: first create an empty object, then call the person method to assign values to it, and finally return the object, we get a per object.

Prototype-the prototype object and prototype attribute are repeatedly mentioned here.

In JavaScript, each object has a prototype attribute that points to a prototype object. we mentioned the process of creating an object with new. In fact, when an empty object is created, new will operate on the prototype attribute of the newly generated object.

Each method has a prototype attribute (because the method itself is also an object). The prototype attribute value of the new object generated by the new operator is consistent with that of the constructor. the prototype attribute of the constructor method points to a prototype object. This prototype object has only one constructor initially, and this constructor attribute points to the method where the prototype attribute is located, see the following figure:

 

 

In this way, when a constructor is used to create a new object, it will obtain all the attributes of the prototype object pointed to by the prototype attribute of the constructor. any operation on the prototype object corresponding to the constructor will be reflected to the object generated by the constructor. All these objects share the properties (including methods) of the prototype object corresponding to the constructor ).

Let's take a look at the specific example:

JS Code

 
 
  1. FunctionPerson (name, sex) {// Constructor
  2. This. Name = Name;
  3. This. Sex = sex;
  4. }
  5. Person. Prototype. Age = 12; // assign a value to the attribute of the prototype object corresponding to the prototype attribute
  6. Person. Prototype. Print =Function() {// Add Method
  7. Alert (This. Name + "_" +This. Sex + "_" +This. Age );
  8. };
  9. VaRP1 =NewPerson ("name1", "male ");
  10. VaRP2 =NewPerson ("name2", "male ");
  11. P1.print (); // name1_male_12
  12. P2.print (); // name2_male_12
  13. Person. Prototype. Age = 18; // change the attribute value of the prototype object. Note that the prototype attribute of the constructor is used.
  14. P1.print (); // name1_male_18
  15. P2.print (); // name2_male_18

So far, we have simulated the implementation of simple classes. We have constructors, class attributes, and class methods to create "instances ". in the following article, we use the name "class" to replace the constructor method. However, this is just a simulation, not a real Object-Oriented "class ".

Before proceeding to the next step, let's take a look at how to change the prototype attribute of an object and how to set the prototype attribute. Note: it may help us to understand the problem: after we create an object, this object will obtain the prototype attribute (including functions and variables) of the constructor, which can be considered as a constructor (class) inherits the functions and variables of the prototype object corresponding to its prototype attribute. That is to say, the prototype object simulates the effect of a super class. let's look at an instance:

JS Code 

 
 
  1. FunctionThe constructor of the person (name, sex) {// person class
  2. This. Name = Name;
  3. This. Sex = sex;
  4. }
  5. Person. Prototype. Age = 12; // assign a value to the attribute of the prototype object corresponding to the prototype attribute of the person class,
  6. // Equivalent to adding an attribute to the parent class of the person class
  7. Person. Prototype. Print =Function() {// Add method for the parent class of the person class
  8. Alert (This. Name + "_" +This. Sex + "_" +This. Age );
  9. };
  10. VaRP1 =NewPerson ("name1", "male"); // The age attribute of P1 inherits the parent class (prototype object) of the Child person class)
  11. VaRP2 =NewPerson ("name2", "male ");
  12. P1.print (); // name1_male_12
  13. P2.print (); // name2_male_12
  14. P1.age = 34; // change the age attribute of the P1 instance
  15. P1.print (); // name1_male_34
  16. P2.print (); // name2_male_12
  17. Person. Prototype. Age = 22; // change the age attribute of the person class's superclass.
  18. P1.print (); // name1_male_34 (the age attribute of P1 has not changed with the prototype attribute)
  19. P2.print (); // name2_male_22 (the age attribute of P2 has changed)
  20. P1.print =Function() {// Change the print method of the P1 object
  21. Alert ("I am p1 ");
  22. }
  23. P1.print (); // I am p1 (the P1 method has changed)
  24. P2.print (); // name2_male_22 (the P2 method has not changed)
  25. Person. Prototype. Print =Function() {// Change the print method of the person superclass
  26. Alert ("new print method! ");
  27. }
  28. P1.print (); // I am p1 (the print method of P1 is still its own method)
  29. P2.print (); // new print method! (The print method of P2 changes with the change of the superclass method)

I have read an article that describes that the prototype attribute of an object in Javascript is equivalent to the static variable in Java and can be shared by all objects in this class. The above example shows that the actual situation is not like this:

In JS, when we create an instance object of a class using the new operator, its methods and attributes actually inherit the prototype attribute of the class, the methods and attributes defined in the prototype attribute of the class can be directly referenced by these instance objects. however, when we re-assign or define the attributes and methods of these instance objects, the attributes or methods of the Instance objects will no longer point to the attributes and methods defined in the prototype attribute of the class, at this time, even if you modify the corresponding method or attribute in the prototype attribute of the class, it does not reflect on the instance object. this explains the example above:

In the beginning, two objects P1 and P2 are generated using the new operator. Their age attributes and print methods both come from the prototype attribute of the person class. then, we modified the age attribute of P1, and then assigned a value (person. prototype. age = 22), the age attribute of P1 does not change, but the age attribute of P2 changes accordingly, because the age attribute of P2 is still derived from the prototype attribute of the person class. the same situation is also reflected in the print method later.

Through the above introduction, we know that the prototype attribute simulates the role of the parent class (superclass) in Javascript, and reflects the object-oriented idea in JS. The prototype attribute
Is critical.

 

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.