function Rectangle(w,h) {
this.width = w;
this.height = h;
this.long = 10;
}
Rectangle.prototype.volume = 20;
Rectangle.prototype.area = function() {return this.width * this.height;};
Rectangle.prototype.area2 = function() {return this.long * this.volume;};
function PositionedRectangle(x, y, w, h) {
    Rectangle.call(this, w, h);
    this.x = x;
    this.y = y;
this.long = 100;
}
PositionedRectangle.prototype = new Rectangle();
delete PositionedRectangle.prototype.width;
delete PositionedRectangle.prototype.height;
delete PositionedRectangle.prototype.long;
delete PositionedRectangle.prototype.volume;
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 r2 = new PositionedRectangle(2,2,2,2);
document.write(r2.contains(3,3) + "<br />");
document.write(r2.height+ "<br />");
document.write(r2.area()+ "<br />");
document.write(r2.area2()+ "<br />");
document.write(r2.x + "," + r2.y + "," + r2.width + "," + r2.height + "<br />");
document.write(r2 instanceof PositionedRectangle && r2 instanceof Rectangle && r2 instanceof Object);delete没有做到删除超类 Rectangle的任何属性,后面r2.heigh,r2.area(),r2.area2()…… 都有结果?求助delete到底做了什么?为什么没有达到删除属性的效果

解决方案 »

  1.   

    <script>
    function t()
    {
    }
    t.prototype.f=function(){};
    var o=new t();
    alert(o.f);
    delete o.constructor.prototype.f;
    alert(o.f);
    </script>现在要考虑的是delete t.prototype 反而是不可以的 实例的o.f调用的是 o.constructor.prototype.f 而不是 t.prototype楼主的疑惑应该可以部分解答了,这个还是跟原型链的概念有关
      

  2.   

    <script>
    function Rectangle(w,h) {
        this.width = w;
        this.height = h;
        this.long = 10;
    }
    Rectangle.prototype.volume = 20;
    Rectangle.prototype.area = function() {return this.width * this.height;};
    Rectangle.prototype.area2 = function() {return this.long * this.volume;};
    function PositionedRectangle(x, y, w, h) {
        Rectangle.call(this, w, h);
        this.x = x;
        this.y = y;
        this.long = 100;
    }
    PositionedRectangle.prototype = new Rectangle();
    delete PositionedRectangle.prototype.width;
    delete PositionedRectangle.prototype.height;
    delete PositionedRectangle.prototype.long;
    delete PositionedRectangle.prototype.constructor.prototype.volume;
    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 r2 = new PositionedRectangle(2,2,2,2);
    document.write(r2.contains(3,3) + "<br />");
    document.write(r2.height+ "<br />");
    document.write(r2.area()+ "<br />");
    document.write(r2.area2()+ "<br />");
    document.write(r2.x + "," + r2.y + "," + r2.width + "," + r2.height + "<br />");
    document.write(r2 instanceof PositionedRectangle && r2 instanceof Rectangle && r2 instanceof Object);alert(r2.volume);</script>实验结果验证了我的想法
      

  3.   

    你只删除了 原型对象里的属性,构造函数里的属性无法这样删除,那怎么删除上面代码中的
    this.width = w;
        this.height = h;
        this.long = 10;
    呢?这些代码引用 JavaScript权威指南 第五版
      

  4.   

    <script>
    function Rectangle(w,h) {
      this.width = w;
      this.height = h;
      this.long = 10;
    }
    Rectangle.prototype.volume = 20;
    Rectangle.prototype.area = function() {return this.width * this.height;};
    Rectangle.prototype.area2 = function() {return this.long * this.volume;};
    function PositionedRectangle(x, y, w, h) {
     // Rectangle.call(this, w, h);
      this.x = x;
      this.y = y;
      this.long = 100;
    }
    PositionedRectangle.prototype = new Rectangle();
    delete PositionedRectangle.prototype.width;
    delete PositionedRectangle.prototype.height;
    delete PositionedRectangle.prototype.long;
    delete PositionedRectangle.prototype.constructor.prototype.width;
    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 r2 = new PositionedRectangle(2,2,2,2);
    document.write(r2.contains(3,3) + "<br />");
    document.write(r2.height+ "<br />");
    document.write(r2.area()+ "<br />");
    document.write(r2.area2()+ "<br />");
    document.write(r2.x + "," + r2.y + "," + r2.width + "," + r2.height + "<br />");
    document.write(r2 instanceof PositionedRectangle && r2 instanceof Rectangle && r2 instanceof Object);alert(r2.width);</script>
      

  5.   

    知道了 你删除了从子类函数调用超类构造函数这一步,我还以为这一步是构造函数链必须的
    还有,根据这段代码的例子,如果你删除了 Rectangle.call(this, w, h);这一步,width,height属性无法被子类函数调用,最后显示的结果就是undefined了,,,这看起来好矛盾,难道JavaScript权威指南例子就是存在这个矛盾的问题……
      

  6.   

    function Rectangle(w,h) {
    this.width = w;
    this.height = h;
    this.long =10;
    }
    Rectangle.prototype.volume = 20;
    Rectangle.prototype.area = function() {return this.width * this.height;};
    Rectangle.prototype.area2 = function() {return this.long * this.volume;};
    function PositionedRectangle(x, y, w, h) {
        Rectangle.call(this, w, h);
        this.x = x;
        this.y = y;
    this.long = 100;
    }
    PositionedRectangle.prototype = new Rectangle();
    PositionedRectangle.prototype.volume = 200;
    //PositionedRectangle.prototype.constructor = PositionedRectangle;
    delete PositionedRectangle.prototype.width;
    delete PositionedRectangle.prototype.height;
    delete PositionedRectangle.prototype.volume;
    delete PositionedRectangle.prototype.long;
    delete PositionedRectangle.long;
    delete PositionedRectangle.prototype.constructor.prototype.width;
    delete PositionedRectangle.prototype.constructor.prototype.volume;
    delete PositionedRectangle.prototype.constructor.prototype.long;
    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 r2 = new PositionedRectangle(2,2,2,2);
    document.write(Rectangle.prototype);//toString()返回
    document.write(r2.contains(3,3) + "<br />");
    document.write("width:" + r2.width+ "<br />" + "volume:" + r2.volume+ "<br />" + "long:" + r2.long );
    document.write(r2.area()+ "<br />");
    document.write("long*volume:" + r2.area2()+ "<br />");
    document.write(r2.x + "," + r2.y + "," + r2.width + "," + r2.height + "<br />");
    document.write(r2 instanceof PositionedRectangle && r2 instanceof Rectangle && r2 instanceof Object);最终这样测试了,Rectangle.call(this, w, h);这一步的参数w,h调用暂时不去管,上面测试结果感觉构造函数里this.Attribute  在构造函数里创建的属性delete无法删除
    delete PositionedRectangle.prototype.xxx 删除的是PositionedRectangle.prototype原型创建的属性
    delete PositionedRectangle.prototype.constructor.prototype.xxx 删除的是Rectangle原型Rectangle.prototype创建的属性
    按照权威指南里源代码注释说:
    // We create this prototype object for inheritance purposes, but we
    // don't actually want to inherit the width and height properties that
    // each Rectangle object has, so delete them from the prototype.
    delete PositionedRectangle.prototype.width;
    delete PositionedRectangle.prototype.height;感觉并没有起到删除从Rectangle继承的属性,不理解注释的意义,PositionedRectangle.prototype里也没有width和height