实在感觉很晦涩 写法也是多种多样的  有例:function ClassA(sColor) {
    this.color = sColor;
    this.sayColor = function () {
        alert(this.color);
    };
}
function ClassB(sColor, sName) {
    this.newMethod = ClassA;
    this.newMethod(sColor);
    delete this.newMethod;    this.name = sName;
    this.sayName = function () {
        alert(this.name);
    };
}
var objA = new ClassA("blue");
var objB = new ClassB("red", "John");
objA.sayColor(); //输出 "blue"
objB.sayColor(); //输出 "red"
objB.sayName(); //输出 "John"不太理解这个delete this.newMethod;  这个指向A的指针都被del了怎么到最后objB可以调用sayColor()?

解决方案 »

  1.   

    关键是这两句:
    this.newMethod = ClassA;
    this.newMethod(sColor);
    相当于指定ClassB.newMethod = ClassA(此处ClassA是函数,也就是ClassB的一个方法),
    然后调用这个方法ClassB.newMethod(),也就是调用ClassA()现在来看函数ClassA的代码:主要是代码中的this的含义,既然函数ClassA是ClassB的一个方法,那函数体内的this就是指代ClassB,这样理解下来,ClassA方法的作用就是给ClassB对象增加了一个属性color,一个方法sayColor()。这样运行ClassA的结果就是ClassB多了一个属性一个方法。接下来delete this.newMethod;
    这句的作用是把ClassB的方法newMethod删掉,操作的结果是ClassB.newMethod == undefined
    但是通过newMethod方法给ClassB增加的新方法(sayColor)新属性(color)并没有变化,所以仍然能够调用ClassB.sayColor()方法。
      

  2.   

    this.newMethod(sColor);
    这句只要调用后,ClassA的方法和属性都会到ClassB中去了。删除:this.newMethod(sColor)就没什么关系了。
      

  3.   

    给你分享一篇好文章 指定比我说的清楚
    http://topic.csdn.net/u/20110623/16/34631c99-a3df-43af-b550-acf5557fa7be.html?87133