《javaScript面向对象编程指南》第二版P182--怎么理解
Shape.prototype.toString=function(){
var const = this.constructor;
return const.uber? this.const.uber.toString()+","+this.name:this.name;
}
源码是:
function Shape(){}
Shape.prototype.name="shape";
Shape.prototype.toString=function(){
var const = this.constructor;
return const.uber? this.const.uber.toString()+","+this.name:this.name;
}
function TwoDShape(){}
var F=function(){};
F.prototype=Shape.prototype;
TwoDShape.prototype=new F();
TwoDShape.prototype.constructor=TwoDShape;
TwoDShape.uber=Shape.prototype;
TwoDShape.prototype.name="2D shape";
function Triangle(side,height) {
this.side=side;
this.height=height;
}
var F=function(){};
F.prototype=TwoDShape.prototype;
Triangle.prototype=new F();
Triangle.prototype.constructor=Triangle;
Triangle.uber=TwoDShape.prototype;
Triangle.prototype.name="Triangle";
Triangle.prototype.getArea=function(){
return this.side*this.height/2;
};
var my=new Triangle(5,10);
my.toString();
橋本奈奈未
相关分类