Basic of javascript object-oriented technology (V)

Source: Internet
Author: User

I have read many introductionsJavascriptThe article on object-oriented technology is dizzy. Why? It's not because it's not well written, but because it's too esoteric. The objects in javascript haven't clearly explained what's going on. As soon as they come up, they go straight to the topic, class/Inheritance/prototype/private variable. As a result, after reading it for a long time, I had a rough understanding and had a good aftertaste. It seems that I did not understand anything.

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 variables/class methods/instance variables/instance methods

First, add the methods that have been previously written:

In javascript, all methods have a call method and an apply method. These two methods can simulate the object call method. The first parameter of the method is the object, followed
The parameter indicates the parameter when the object calls this method. For example, we define a method f () and then call the following statement:

 
 
  1. f.call(o, 1, 2); 

The function is equivalent

 
 
  1. o.m = f;  
  2. o.m(1,2);  
  3. delete o.m; 

For example:

Js Code:

 
 
  1. Function Person (name, age) {// define a method
  2. This. name = name;
  3. This. age = age;
  4. }
  5. Var o = new Object (); // empty Object
  6. Alert (o. name + "_" + o. age); // undefined_undefined
  7. Person. call (o, "sdcyst", 18); // call: o. Person ("sdcyst", 18)
  8. Alert (o. name + "_" + o. age); // sdcyst_18
  9. Person. apply (o, ["name", 89]); // The apply method is used in the same way as the call method. The difference is that the input parameters are transmitted using arrays.
  10. Alert (o. name + "_" + o. age); // name_89

Both instance variables and instance methods are accessed by adding the "." operator to the Instance Object and following the attribute name or method name. However, you can also set methods or variables for classes,
In this way, you can directly use the class name plus "." operator and then follow the property name or method name to access. Defining class attributes and class methods is very simple:

Js Code

 
 
  1. Person. counter = 0; // defines the class variable, the number of Person instances created
  2. Function Person (name, age ){
  3. This. name = name;
  4. This. age = age;
  5. Person. counter ++; // no instance is created, class variable counter plus 1
  6. };
  7. Person. whoIsOlder = function (p1, p2) {// class method to determine who is older
  8. If (p1.age> p2.age ){
  9. Return p1;
  10. } Else {
  11. Return p2;
  12. }
  13. }
  14. Var p1 = new Person ("p1", 18 );
  15. Var p2 = new Person ("p2", 22 );
  16. Alert ("now" + Person. counter + "individual"); // now there are two persons
  17. Var p = Person. whoIsOlder (p1, p2 );
  18. Alert (p. name + "older"); // p2 older

Application of the prototype attribute:

The following example is based on the original book. Suppose we have defined a Circle class, which has a radius attribute and the area method. The implementation is as follows:

Js Code:

 
 
  1. function Circle(radius) {  
  2. this.radius = radius;  
  3. this.area = function() {  
  4. return 3.14 * this.radius * this.radius;  
  5. }  
  6. }  
  7. var c = new Circle(1);  
  8. alert(c.area()); //3.14 

Suppose we have defined 100 Circle class instance objects, then each instance object has a radius attribute and area method. In fact, apart from the radius attribute, the area Method of each Circle class instance object is the same. In this way, we can extract the area Method and define it in the prototype attribute of the Circle class, in this way, all instance objects can call this method to save space.

Js Code

 
 
  1. function Circle(radius) {  
  2. this.radius = radius;  
  3. }  
  4. Circle.prototype.area = function() {  
  5. return 3.14 * this.radius * this.radius;  
  6. }  
  7. var c = new Circle(1);  
  8. alert(c.area()); //3.14  

Now, let's use the prototype attribute to simulate class inheritance: first define a Circle class as the parent class, and then define the sub-class PositionCircle.

Js Code
 

 
 
  1. Function Circle (radius) {// defines the parent class Circle
  2. This. radius = radius;
  3. }
  4. Circle. prototype. area = function () {// defines the method of the parent class. area: calculates the area.
  5. Return this. radius * this. radius * 3.14;
  6. }
  7. Function PositionCircle (x, y, radius) {// defines the class PositionCircle
  8. This. x = x; // The x coordinate of the attribute.
  9. This. y = y; // attribute ordinate
  10. Circle. call (this, radius); // call the method of the parent class, which is equivalent to calling this. Circle (radius) and setting
  11. // Radius attributes
  12. }
  13. PositionCircle. prototype = new Circle (); // you can specify the parent class of PositionCircle as the Circle class.
  14. Var pc = new PositionCircle (1, 2, 1 );
  15. Alert (pc. area (); // 3.14
  16. // The area Method of the PositionCircle class inherits from the Circle class, while the area method of the Circle class
  17. // The area Method inherits the prototype object corresponding to its prototype attribute.
  18. Alert (pc. radius); // The radius attribute of the PositionCircle class inherits from the Circle class.
  19. /* Note: we set the prototype attribute of the PositionCircle class to point to a Circle object. Therefore, the prototype attribute of the pc inherits the prototype attribute of the Circle object, the constructor attribute of the Circle object (that is, the constructor attribute of the prototype object corresponding to the Circle object) is directed to the Circle object.
  20. Is Circ.
  21. */
  22. Alert (pc. constructor); // Circle
  23. /* For this reason, after we have designed the inheritance relationship of the class, we also need to set the constructor attribute of the subclass. Otherwise, it will point to the parent class.
  24. Constructor attribute
  25. */
  26. PositionCircle. prototype. constructor = PositionCircle
  27. Alert (pc. constructor); // PositionCircle

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.