请老师讲三行代码,谢谢。function Father() {
  this.name = 'father';
}function Son() {
  this.init2 = Father;
  this.init2();
  delete this.init2;
}就是这三行:
  this.init2 = Father;
  this.init2();
  delete this.init2;
不明白,关键是为何要
delete this.init2
呢?

解决方案 »

  1.   

    手册中是这样说的delete 运算符
    从对象中删除一个属性,或从数组中删除一个元素。delete expressionexpression 参数是一个有效的 JScript 表达式,通常是一个属性名或数组元素。 说明
    如果 expression 的结果是一个对象,且在 expression 中指定的属性存在,而该对象又不允许它被删除,则返回 false。在所有其他情况下,返回 true。 
      

  2.   

    this.init2 = Father;//将Father函数赋值给init2,this代表window
    this.init2();//调用init2
    delete this.init2;//删除init2
    <script language="JavaScript">
    function Father() {
      this.name = 'father';
    }function Son() {
      this.init2 = Father;
      alert(this.init2);
      this.init2();
      delete this.init2;
      alert(this.init2);
    }
    Son();
    </script>
    看这个例子就明白了,测试在ff中第二次输出为undefined,ie6中测试没有第二次输出
      

  3.   

    function Father() {
      this.name = 'father';
    }function Son() {
      this.init2 = Father;//实例化Son对象,将其init2属性赋值Father
      this.init2();       //相当于从Father继承的构造函数,此时Father中的this对象为Son的某个实例化对象
      delete this.init2;  //删除构造函数
    }
    var s = new Son();    //使用new 关键字实例化Son类