SoFunction
Updated on 2025-04-14

A good JavaScript object-oriented simple introduction to page 2/2


4) Method
The following example is quoted for <<__JavaScript: The Definitive Guide>>
function Rectangle_area(  ) { return  * ; }
function Rectangle_perimeter(  ) { return 2* + 2*; }
function Rectangle_set_size(w,h) {  = w;  = h; }
function Rectangle_enlarge(  ) {  *= 2;  *= 2; }
function Rectangle_shrink(  ) {  /= 2;  /= 2; }
function Rectangle(w, h)
{
     = w;
     = h;
     = Rectangle_area;
     = Rectangle_perimeter;
    this.set_size = Rectangle_set_size;
     = Rectangle_enlarge;
     = Rectangle_shrink;
}
This style may be very different from Java and C++. This in the method represents a reference to the object that calls it.


5) prototype
prototype is an object, each class contains a prototype object (note, one for each class, not one for each object).
Take a look at the following example:
function User(name)
{
     = name
}
= "killercat"  // Class name.prototype.Properties (or methods)
user = new User("who"+"<br />")
()
delete 
()

Let’s take a look at another example:
function User(name)
{
}
 = "human"
user1 = new User()
user2 = new User()
(+"<br />")
()
result:
human
human
It is explained that each class has a prototype object, not a separate one for each object.

The search order of this statement is to first look for the x attribute in obj. If not, then go to the corresponding class of obj to look for it. The same is true for the method. Therefore, do not appear such a statement: = "xxx" must be = "xxx" (the prototype object belongs to a class, not an object)

Class name.prototype.Properties  // Equivalent to an instance variable (property), and the same is true for methods
Class name. Attribute  // Equivalent to a static variable (property), the same is true for methods. "Class name. Attribute" must be used when calling, and "Class object. Attribute" cannot be used because it belongs to a class, not an object.
For example:
function User(name)
{
     = name
}
 = "human"

user = new User("kc")
( + "<br />")
()
result:
human
undefined

In addition, each prototype has a constructor attribute, which is used by default to save the definition of the constructor, such as the user object above, and calls:
get:
function User(name) {  = name; }
We can use typeof to know the type of the parameter. If it is an object, it will return "object", and if it is a method, it will return "function"

6) Use prototype to implement inheritance between classes, for example:
// Parent class
function Circle(r){
     = r;

}
 = 3.14;
 = function (){   
    return  *  * ;
};
 = function (){   
    if(( typeof this == "object") && ( == Circle)){
        return "circle with a radius " +  ;
    }
    else{
        return "unknown object";
    }   
};
 = function (c1,c2){
    return  >=  ? c1 : c2;
};

// Subclass
function ColorCircle(r,color){
     = r;
     = color;
}
= new Circle(0);  // Save the object of the parent class
= ColorCircle;  // Change the name of the constructor
 = function(){
    if(( typeof this == "object") && ( == ColorCircle)){
        return +" circle with a radius " +  ;
    }
    else{
        return "unknown object";
    }   
}
 = function(){
    return ;
}
 = function(color){
     = color;
}

That is, use prototype to save the object of the parent class. When constructing the subclass, the parent class object is constructed at the same time (because prototype is constructed). That is, JavaScript inheritance is actually to let the subclass prototype object save the parent class object.
Previous page12Read the full text