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
Parameter indicates the parameter when the object calls this method (ecmascript specifies two methods that are defined for all functions, call ()
And apply (). These methods allow you to invoke a function as if it were a method of some other object. The first
Argument to both call () and apply () is the object on which the function is to be invoked; this argument becomes
The value of the this keyword within the body of the function. Any remaining arguments to call () are the values
That are passed to the function that is invoked). For example, we define a method F () and then call the following statement:
F. Call (O, 1, 2 );
The function is equivalent
O. M = F;
O. m (1, 2 );
Delete o. m;
For example:
JS Code
- FunctionPerson (name, age ){// Define the Method
-
- This. Name = Name;
-
- This. Age = age;
-
- }
-
- VaRO =NewObject ();// Empty object
-
- Alert (O. Name +"_"+ O. Age );// Undefined_undefined
-
- Person. Call (O,"Sdcyst", 18 );// Call: O. person ("sdcyst", 18)
-
- Alert (O. Name +"_"+ O. Age );// Sdcyst_18
-
-
- Person. Apply (O ,["Name", 89]);// The apply method is used in the same way as the call method. The difference is that the parameters are transmitted using arrays.
-
- Alert (O. Name +"_"+ O. Age );// Name_89
Function person (name, age) {// defines the method this. name = Name; this. age = age;} var o = new object (); // empty object alert (O. name + "_" + O. age); // undefined_undefinedperson.call (O, "sdcyst", 18); // call: O. person ("sdcyst", 18) Alert (O. name + "_" + O. age); // sdcyst_18person.apply (O, ["name", 89]); // The apply method works the same as call, the difference is that the form of passing parameters is to use arrays to pass 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
-
- Person. Counter = 0;// Define the class variable and the number of person instances created
-
- FunctionPerson (name, age ){
- This. Name = Name;
-
- This. Age = age;
-
- Person. Counter ++;// No instance is created, class variable counter plus 1
-
- };
-
-
- Person. whoisolder =Function(P1, P2 ){// Class method to determine who is older
-
- If(P1.age> p2.age ){
- ReturnP1;
-
- }Else{
-
- ReturnP2;
-
- }
-
- }
-
-
- VaRP1 =NewPerson ("P1", 18 );
- VaRP2 =NewPerson ("P2", 22 );
-
-
- Alert ("Now"+ Person. Counter +"Individual");// There are two people now
-
- VaRP = person. whoisolder (P1, P2 );
-
- Alert (P. Name +"Older");// P2 is older
Person. counter = 0; // defines the class variable. The number of created person instances function person (name, age) {This. name = Name; this. age = age; person. counter ++; // no instance is created, class variable counter plus 1}; person. whoisolder = function (P1, P2) {// class method to determine who is older if (p1.age> p2.age) {return P1 ;}else {return P2 ;}} vaR p1 = new person ("p1", 18); var P2 = new person ("p2", 22); alert ("now" + person. counter + "individual"); // now there are two individuals var P = person. whoisolder (P1, P2); alert (P. name + "older"); // P2 older
Application of the prototype attribute:
The following example is based on the original book.
Suppose we define a circle class, which has a radius attribute and an area method. The implementation is as follows:
JS Code
-
- FunctionCircle (RADIUS ){
-
- This. Radius = radius;
- This. Area =Function(){
-
- Return3.14 *This. Radius *This. Radius;
-
- }
-
- }
-
- VaRC =NewCircle (1 );
-
- Alert (C. Area ());// 3.14
Function circle (RADIUS) {This. radius = radius; this. area = function () {return 3.14 * This. radius * This. radius ;}} var c = new circle (1); 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, except for the radius attribute, the Area Method of each circle class instance object is the same. In this way, we can
Define the Area Method in the prototype attribute of the circle class so that all instance objects can call this method,
This saves space.
JS Code
-
- FunctionCircle (RADIUS ){
-
- This. Radius = radius;
-
- }
-
- Circle. Prototype. Area =Function(){
- Return3.14 *This. Radius *This. Radius;
-
- }
-
- VaRC =NewCircle (1 );
-
- Alert (C. Area ());// 3.14
Function circle (RADIUS) {This. radius = radius;} circle. prototype. area = function () {return 3.14 * This. radius * This. radius;} var c = new circle (1); 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 subclass
Positioncircle.
JS Code
-
- FunctionCircle (RADIUS ){// Define the parent class circle
-
- This. Radius = radius;
-
- }
-
- Circle. Prototype. Area =Function(){// Define the parent class method area to calculate the area
-
- Return This. Radius *This. Radius * 3.14;
- }
-
-
- FunctionPositioncircle (X, Y, radius ){// Define the class positioncircle
-
- This. X = X;// Horizontal coordinate of the attribute
-
- This. Y = y;// Attribute ordinate
- Circle. Call (This, Radius );// Call the method of the parent class, which is equivalent to calling this. Circle (RADIUS) and setting the positioncircle class
-
- // RADIUS attributes
-
- }
-
- Positioncircle. Prototype =NewCircle ();// Set positioncircle's parent class to circle class
-
- VaRPC =NewPositioncircle (1, 2 );
-
- Alert (PC. Area ());// 3.14
-
- // The Area Method of the positioncircle class inherits from the circle class, while the area method of the circle class
-
- // The Area Method inherits the prototype object corresponding to its prototype attribute.
-
- Alert (PC. radius );// 1 the radius attribute of the positioncircle class inherits from the circle class
-
- /*
-
- Note: Previously we set the prototype attribute of the positioncircle class to point to a circle object,
-
- Therefore, the prototype attribute of PC inherits the prototype attribute of the circle object, while the constructor of the circle object belongs
-
- (That is, the constructor attribute of the prototype object corresponding to the Circle object) is directed to the circle.
-
- Is CIRC.
-
- */
-
- Alert (PC. constructor );// Circle
-
-
- /* 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.
- Constructor attribute
-
- */
-
- Positioncircle. Prototype. constructor = positioncircle
-
- Alert (PC. constructor );// Positioncircle