问题很简单;
你的代码只是复制了ClassA的代码到this.newMethod了;
你的ClassB就变成了这种形式;function   ClassB(sColor,sName){ 
    //复制了函数
    this.newMethod   =   function(sColor){
        this.color   =   sColor; //这里的this是ClassB;
        this.sayColor   =   function(){ 
            alert(this.color); 
        };
    }; 
    //执行函数只是修改了ClassB;获得了color属性与sayColor方法;
    this.newMethod(sColor); 
    delete   this.newMethod; //只是删除掉newMethod函数;但此时ClassB以改变。    this.name   =   sName; 
    this.sayName   =   function(){ 
        alert(this.name); 
    }; 
} 肯定会要执行sayColor了;因为此时sayColor以经是ClassB的一个方法而已;

解决方案 »

  1.   


    <html> 
    <head> 
    <title> array   prototypes </title> 
    <script   language="javascript"> 
    function   ClassA(sColor){ 
    this.color   =   sColor; 
    this.sayColor   =   function(){ 
    alert(this.color); 

    } function   ClassB(sColor,sName){ 
    this.newMethod = ClassA;
    //这句相当于
    /*
    this.newMethod = function (sColor){
    //这里的this自然就是实例了
    this.color   =   sColor; 
    this.sayColor   =   function(){ 
    alert(this.color); 
    }
    }
    */
    this.newMethod(sColor); 
    delete this.newMethod; //这句成功了。。它把this.newMethod成员给删除掉了。。this.name   =   sName; 
    this.sayName   =   function(){ 
    alert(this.name); 
    };

    </script> 
    <body> 
    <script   language="javascript"> 
    var   objA=new ClassA("red"); 
    var   objB=new ClassB("blue","nichola"); 
    objA.sayColor(); 
    objB.sayColor();
    objB.sayName();
    </script>
    </body>
    </html>
    如果只是实现这样的功能的话,直接用call或者apply就可以了
      

  2.   

    刚才发了一次竟然没发成功。
    再发一次;
    delete  删除的只是newMethod;在这句后面就不能再访问newMethod了。
    但是你运行了newMethod就是把ClassB改变了。
    要注意newMethod在删除前是ClassB的一个成员函数。相当于:function   ClassB(sColor,sName){ 
        this.newMethod   =   function(sColor){
           this.color   =   sColor; //这里的this是ClassB;添加一个属性;
           this.sayColor   =   function(){ //给ClassB添加一个函数;
               alert(this.color); 
           };
        }; 
        this.newMethod(sColor); //这句修改了ClassB;
        delete   this.newMethod; //删除成员函数newMethod;    this.name   =   sName; 
        this.sayName   =   function(){ 
            alert(this.name); 
        }; 

      

  3.   

    刚才发了一次竟然没发成功。
    再发一次;
    delete  删除的只是newMethod;在这句后面就不能再访问newMethod了。
    但是你运行了newMethod就是把ClassB改变了。
    要注意newMethod在删除前是ClassB的一个成员函数。相当于:function   ClassB(sColor,sName){ 
        this.newMethod   =   function(sColor){
           this.color   =   sColor; //这里的this是ClassB;添加一个属性;
           this.sayColor   =   function(){ //给ClassB添加一个函数;
               alert(this.color); 
           };
        }; 
        this.newMethod(sColor); //这句修改了ClassB;
        delete   this.newMethod; //删除成员函数newMethod;    this.name   =   sName; 
        this.sayName   =   function(){ 
            alert(this.name); 
        }; 

      

  4.   

    1
    this.newMethod = ClassA; 
    this.newMethod(sColor);
    的作用就是把sayColor方法“复制”给了ClassB
    所以ClassB调用的sayColor方法是自己的方法而不是ClassA的delete this.newMethod;
    跟这样的作用差不多 this.newMethod=null;
    目的就是断开ClassA和ClassB的关联
    防止ClassB对ClassA进行修改2
    this.newMethod = ClassA; 
    this.newMethod(sColor);
    delete this.newMethod;
    其实是这样的过程
    var a = new ClassA(sColor);
    this.color = a.color;
    this.sayColor = a.sayColor;
    这里并不是传统意义的“继承”而更像是“复制”
      

  5.   

    嗯,几句话解大吾大悟呀!3Q各位朋友!尤其perry_peng真是热心嘬!