小弟最近在学习JavaScript权威指南那本书,看到继承那里看不懂了
    //
                   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());小弟原来也看过JavaScript高级程序设计,也没见过这么写的。那个delete那里有些不解,我把delete屏蔽了,还是得不到r.width的值嘛。还有那个PositionedRectangle.prototype.constructor = PositionedRectangle也很不理解。难道是制定构造函数为自己?怎么好像去了也没什么关系。
请大家稍微为小弟指点下即可,谢谢大家

解决方案 »

  1.   

    delete PositionedRectangle.prototype.width
    原型连上没有,删他没作用,
    PositionedRectangle.prototype.constructor = PositionedRectangle
    当一个构造函数的原型改变了,他的构造器也变了,所以要给他指定回去,但你要用不到就没必要了
      

  2.   

     function PositionedRectangle(x,y,w,h){
                //
                Rectangle.call(this,w,h);
                this.x = x;
                this.y = y;
            }
            //
            PositionedRectangle.prototype = new Rectangle();
    原来还可以这样写继承
      

  3.   

            delete PositionedRectangle.prototype.width;
            delete PositionedRectangle.prototype.height;这里删除的意思应该是为了节省资源  不需要保留2个用不着的属性解释 :
            function PositionedRectangle(x,y,w,h){
                Rectangle.call(this,w,h);
                this.x = x;
                this.y = y;
            }可以看到   Rectangle.call(this,w,h);
    所以 在这个地方 会有width  和 height 的属性会指向实例化的对象.然后PositionedRectangle.prototype = new Rectangle();
    这个原形上又会有width 和 height属性  但是没有直;
    因为PositionedRectangle本身就有 this.width this.height的属性
    自然 永远不会查找到 原型链上面去  所以就把原型链上面的删除掉了可以测试一下
            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个属性就永远访问不到  不如删除了节省资源
    */