<script language="javascript">
function person(name){
    this.name = name;    
}
person.prototype.say = function(){alert("hello, i'm " + this.name);}function employee(name, salary){
    person.call(this, name);
    this.salary = salary;
}
employee.prototype = new person();    //为什么要写这句?employee.prototype.showMoney = function(){alert(this.name + "'s salary is " + this.salary);}alert(employee.prototype == person.prototype);
alert(employee.prototype == person);
</script>上面不是有person.call(this, name)这句吗?这句不久可以直接从上面的函数继承的吗?为什么还要加employee.prototype = new person()这句?这句和person.call(this, name)有什么不同?

解决方案 »

  1.   

    this.name = 'aaa';
    person.call(this, name); 继承类的实例的属性和方法person.prototype.name2 = 'bbb'
    employee.prototype = new person(); 继承类的原型的属性和方法
      

  2.   

    当然有不同啊一个是在原形上
    一个是在本身的属性上如新生成的对象 var a = new employee('haha')a可以看成
    {
     name:'haha',
     __proto__ :{
       name:undefined
    }
    }一个例子
    Object.prototype.name="hehea"  //为了方便查看 Object的原形上也加了name属性
    function person(name){
      this.name = name;   
    }
    person.prototype.say = function(){alert("hello, i'm " + this.name);}function employee(name, salary){
      person.call(this, name);
      this.salary = salary;
    }
    employee.prototype = new person(); //为什么要写这句?employee.prototype.showMoney = function(){alert(this.name + "'s salary is " + this.salary);}
    var a = new employee('haha')alert(a.name);  
    delete a.name;
    alert(a.name)
    delete employee.prototype.name
    alert(a.name)
      

  3.   

    call()继承基类的属性,用prototype 继承基类方法
      

  4.   


    应该不是这样的吧?call()也可以继承方法,prototype也可以继承属性啊
      

  5.   

    this.name = 'aaa';
    person.call(this, name); 继承类的实例的属性和方法person.prototype.name2 = 'bbb'
    employee.prototype = new person(); 继承类的原型的属性和方法
      

  6.   

    这个我以为LZ是对JS的继承有了解的LZ去看看call的用法和prototype的意义就知道了。