87,901
社区成员
发帖
与我相关
我的任务
分享
//
function Rectangle(w,h){
this.width = w;
this.height = h;
}
//
Rectangle.prototype.area = function(){
return this.width*this.height;
}
//
function PositionedRectangle(x,y,w,h){
//
Rectangle.call(this,w,h);
this.x = x;
this.y = y;
}
//
PositionedRectangle.prototype = new Rectangle();
//
delete PositionedRectangle.prototype.width;
delete PositionedRectangle.prototype.height;
//
PositionedRectangle.prototype.constructor = PositionedRectangle;
//
PositionedRectangle.prototype.contains = function(x,y){
return (x > this.x && x < this.x + this.width && y > this.y && y < this.y + this.height);
}
//测试
var r = new PositionedRectangle(2,2,2,2);
alert(r.area());
function Rectangle(w,h){
this.width = w;
this.height = h;
}
//
Rectangle.prototype.area = function(){
return this.width*this.height;
}
//
function PositionedRectangle(x,y,w,h){
//Rectangle.call(this,w,h); //这个地方不要 this.width this.height
this.x = x;
this.y = y;
}
//
PositionedRectangle.prototype = new Rectangle(10,15); //在原型上设置width height 分别为 10 15
//
//delete PositionedRectangle.prototype.width; 不删除原型上的width属性
//delete PositionedRectangle.prototype.height;
//
PositionedRectangle.prototype.constructor = PositionedRectangle;
//
PositionedRectangle.prototype.contains = function(x,y){
return (x > this.x && x < this.x + this.width && y > this.y && y < this.y + this.height);
}
var r = new PositionedRectangle(2,2,2,2);
alert(r.width) //可以看到是10 是重原型上得到的
alert(r.height) //这里是15
alert(r.area());
/*
重上面可以看到
如果写了Rectangle.call(this,w,h);
生成实例中就会有width height 不会重原形链上查找了 那么原型链上的2个属性就永远访问不到 不如删除了节省资源
*/