var ListData = {
   name:"John",
   sayName:function(){
      alert(this.name)
   }
}
var ListData2 = {
   name:"John2",
}
ListData.call(ListData2);
ListData2.sayName();
也就是让ListData2 继承ListData,并可以调用ListData的方法,为什么错了?求解释!

解决方案 »

  1.   

    var ListData = function()
    {
        this.name="john";
        this.sayName = function()
        {
            alert(this.name)
        }
    }var ListData2 = function()
    {
         this.name="john2"
    }ListData2.prototype = new ListData();var listData2 = new ListData2()
    listData2.sayName() 
    这样是继承
      

  2.   

    call也不是这样用的啊,他是改变函数的调用对象也就是函数有call方法 ,对象没有
      

  3.   

    这是最常见的基于原型prototype的继承例子 楼上沙发很快  废话 我也就不多说了 LZ先找点继承的资料看看 那里边对call的运用很多 call的是方法 而不是对象
      

  4.   

    function  Parent(){
       this.name = "Parent";
       this.sayName = function(){
           alert(this.name)
       }
    }
    function Child(){
    this.name = "Child";
    Parent.call(this);
    }
    var child = new Child();child.sayName();为什么这样可以?
    function  Parent(){
       this.name = "Parent";
       this.sayName = function(){
           alert(this.name)
       }
    }
    function Child(){
    this.name = "Child";}
    Parent.call(Child);
    var child = new Child();
    child.sayName();这样就不行?
      

  5.   

    调用call(this)就是想把Parent中执行的this,绑定到Child的this上面。也就是想绑定到Child的对象上面
    Parent.call(Child);这句是绑定到Child类上面,当然意思不一样
      

  6.   

    function Parent(){
      this.name = "Parent";
      this.sayName = function(){
      alert(this.name)
      }
    }
    function Child(){
    this.name = "Child"; }
    Parent.call(Child);
    alert(Child.name);
    所以这样就能打印出来了
      

  7.   

    还是没有理解 call的第一个参数是对象 用这个对象改变你call的那个函数的作用域
    function Parent(){
      this.name = "Parent";
      this.sayName = function(){
      alert(this.name)
      }
    }
    function Child(){
    this.name = "Child";}
    Parent.call(Child); //你这样相当于给window对象的Child定义了一个name和sayName方法 因为js中函数也是一个对象
    var child = new Child();//new这个 这回走Child()函数 其他的什么都没做
    child.sayName(); // 这句话就错了  你换成 alert(window.Child.sayName()) 应该有alert值的 你就知道了
     
      

  8.   


    不是这样继承的,继承要用prototype
      

  9.   

    这个能合并 没听说过能继承..再说call也不是这样用的!
    楼主要的是不是这意思?var ListData = {
      name:"John",
      sayName:function(){
      alert(this.name)
      }
    }
    var ListData2 = {
      name:"John2",
    }Object.extend=function(oldobj,newobj){
       for(var name in ListData  ){
        newobj[name]=oldobj[name];
       }
     return newobj[name];
    };
    Object.extend(ListData ,ListData2);
    //这样ListData2就有sayName了
    ListData2.sayName();另外建议楼主google一下call的用法吧!
      

  10.   

    楼上代码有点小错误,改正后:var ListData = {
      name:"John",
      sayName:function(){
      alert(this.name)
      }
    }
    var ListData2 = {
      name:"John2",
    }Object.extend=function(oldobj,newobj){
       for(var name in ListData  ){
        newobj[name]=oldobj[name];
       }
     return newobj;
    };
    Object.extend(ListData ,ListData2);
    //这样ListData2就有sayName了
    ListData2.sayName();
      

  11.   

    根据最新的ECMAScript5标准,lz这样的就是类
    通过Object.create方法实例化。
    可以看我的博文
    http://www.iteye.com/topic/892002
      

  12.   

    <script language=javascript>
    function Car(Color)
    {
     this.color=Color;
    }
    car.prototype.saycolor = function() {alert(this.color);};function CarA(Color,Name)
    {
     Car.call(this,Color);
     this.name = Name;
    }
    CarA.prototype = new Car();
    CarA.prototype.sayname = function() {alert(this.name);};//实现继承CarA继承Car
    </script>
      

  13.   

    call  函数的方法function a(name){
    this.name=name;
    }function b(name,age){
    this.age=age;
    //他继承a()
    a.call(this,name,age)
    }
    var n = new b("c",18);
    alert(n.name+"\n"+n.age)当然没有参数可以直接用prototype
      

  14.   

    function Test(){
       this.say = function(){
         alert(this.say.caller)
       }
          this.say();
    }
    Test();为什么弹出来的是Test的整个代码,而不是Test这个名字?