思路是在继承的类Rectangle上实现Shape中的calculateSurface方法,用Rectangle类上传递的参数计算曲面。
function Shape (w,h){
this.h = h;
this.w = w;
this.calculateSurface = function (){
return this.w*this.h;
};
}
function Rectangle (w,h){
Shape.apply(this, arguments);
this.w = w;
this.h = h;
this.calcSurface = function(){
return Shape.calculateSurface(this.w, this.h);
};
}
Rectangle.prototype = new Shape();
Rectangle.prototype.constructor = Rectangle;
var rec = new Rectangle(4,4);
console.log(rec.calcSurface());
我得到的错误是:
Uncaught TypeError: Object function Shape(w,h){
this.h = h;
this.w = w;
this.calculateSurface = function (){
return this.w*this.h;
};
} has no method 'calculateSurface'