function Rectangle(w,h){
this.width = w; //代表矩形的宽
this.height= h; //代表矩形的高
}
Rectangle.prototype.area=function(){//此定义是构造函数原型对象的一个属性,它代表面积
return this.width * this.height;
}
Rectangle.prototype.toString=function(){//定义一个toString的原型方法
return "["+this.width+","+this.height+"]";
}
Define a subclass constructor to succeed to the father of Class: javascript inheritance introduce specific http://han2000lei.javaeye.com/blog/323506
function PositionedRectangle(x,y,w,h){
Rectangle.call(this,w,h); //使用函数的call()方法,以便初始化父类构造函数中的属性
this.x = x;
this.y = y;
}
PositionedRectangle.prototype = new Rectangle();
delete PositionedRectangle.prototype.width;
delete PositionedRectangle.prototype.height;
PositionedRectangle.prototype.constructor = PositionedRectangle;
PositionedRectangle.prototype.toString =function(){//重写父类中的toString方法
return "["+this.x+","+this.y+"]";
} We create an instance of subclass, and call the subclass of the toString () method
var pr = new PositionedRedtangle(2,2,8,3); pr.toString();
Do not explain the above code, the sub-class call its own toString () is the implementation of the following sub-class toString () method. Ways in our rewrite, the main functions are in order to expand, rather than complete coverage of the parent class method, then how can we call the parent class of this method? The following code:
PositionedRectangle.prototype.toString=function(){
return "("+this.x+","+this.y+")"+Rectangle.prototype.toString.apply(this);
} In this way, the use of apply () method, which calls the parent class toString () method. Specifically apply () method of the role of reference My Documents http://han2000lei.javaeye.com/blog/324680







