function Shape(){};
Shape.prototype.name = "shape";
Shape.prototype.toString = function(){return this.name;};
function TwoDShape(){};
TwoDShape.prototype = new Shape();
TwoDShape.prototype.constructor = TwoDShape; //constructor属性进行重置
TwoDShape.prototype.name = "2D shape";
function Triangle(side,height){
this.side = side;
this.height = height;
}
Triangle.prototype = new TwoDShape();
Triangle.prototype.constructor = Triangle; //constructor属性进行重置
Triangle.prototype.name = "Triangle";
Triangle.prototype.getArea = function(){return this.side*this.height};
var my = new Triangle(5,10);
my.getArea();
my.name;
可以通过hasOwnProperty方法来检测对象的自身属性和原型链属性 自身属性返回true 原型链属性返回false;
my.hasOwnProperty("side"); //true, side是自身属性
my.hasOwnProperty("name"); //false, name是原型链属性
将共享属性迁移到原型中去,方法和属性的查找工作将更多的发生在原型链中
Shape.prototype.name = "shape";
Shape.prototype.toString = function(){return this.name;};
function TwoDShape(){};
TwoDShape.prototype = new Shape();
TwoDShape.prototype.constructor = TwoDShape; //constructor属性进行重置
TwoDShape.prototype.name = "2D shape";
function Triangle(side,height){
this.side = side;
this.height = height;
}
Triangle.prototype = new TwoDShape();
Triangle.prototype.constructor = Triangle; //constructor属性进行重置
Triangle.prototype.name = "Triangle";
Triangle.prototype.getArea = function(){return this.side*this.height};
var my = new Triangle(5,10);
my.getArea();
my.name;
可以通过hasOwnProperty方法来检测对象的自身属性和原型链属性 自身属性返回true 原型链属性返回false;
my.hasOwnProperty("side"); //true, side是自身属性
my.hasOwnProperty("name"); //false, name是原型链属性
将共享属性迁移到原型中去,方法和属性的查找工作将更多的发生在原型链中