var Point = function() {
this. x = 1;
this. y = 1;
this.setPoint = function(px, py) { x = px; y = py; };
this.showPoint = function() { alert("x=\t" + x + "\ny=\t" + y); };
};
var ColorPoint = function(){ this.color = "#FFFFFF";
Point.call(this); //this 指的是Obj
};
var p1= new ColorPoint();
p1.setPoint(5,5);
p1.showPoint(); //这里是 "x = 5   y = 5"
alert(p1.x);   //这里是 1  //没想到啊!
//气死我了,没想到啊!

解决方案 »

  1.   

        this.setPoint = function(px, py) { this.x = px; this.y = py; };
        this.showPoint = function() { alert("x=\t" + this.x + "\ny=\t" + this.y); };
      

  2.   

    你这样子搞,x和y都是全局变量,因为你Point函数里没有初始化。
      

  3.   

    上面 牛B!原来如此!!!
    在最后加一行,果然是这个样子
     alert("window.x==="+window.x+";window.y==="+window.y);//windowx===5;window.y===5
    你说,我要初始化,可是怎么个初法?
      

  4.   

    再 啰嗦一句:
       Point.call(this); //this 指的是Obj
     这一行,js 究竟干了什么?是这样吗?下面的 两种等效吗?
    var Point = function() {
    this. x = 1;
    this. y = 1;
    this.setPoint = function(px, py) { this.x = px; this.y = py; };
    this.showPoint = function() { alert("x=\t" + this.x + "\ny=\t" + this.y); };
    };
    var ColorPoint = function(){ this.color = "#FFFFFF";
    Point.call(this); //this 指的是Obj
    };
    var ColorPoint = function(){
    this.color = "#FFFFFF";
    //Point.call(this); //这删了这行,加了下面那一句.
    (function() {
    this. x = 1;
    this. y = 1;
    this.setPoint = function(px, py) { this.x = px; this.y = py; };
    this.showPoint = function() { alert("x=\t" + this.x + "\ny=\t" + this.y); };
    })();
    };
      

  5.   

    不等效。function() {
            this. x = 1;
            this. y = 1;
            this.setPoint = function(px, py) { this.x = px; this.y = py; };
            this.showPoint = function() { alert("x=\t" + this.x + "\ny=\t" + this.y); };
    }这部分其实是一个构造函数,只能用new构造对象,而不能直接执行。其实call就是让一方享有另一方的数据和方法,所以一定要说等效的话就是下面这样子:var ColorPoint = function(){    this.color = "#FFFFFF";
        this. x = 1;
        this. y = 1;
        this.setPoint = function(px, py) { this.x = px; this.y = py; };
        this.showPoint = function() { alert("x=\t" + this.x + "\ny=\t" + this.y); };
    };
      

  6.   

    其实你这个继承的实现模式没有错,这个模式是借用构造函数来实现继承的。
    错在this.x和x的理解。var Point = function() {
        this. x = 1;
        this. y = 1;
        this.setPoint = function(px, py) { x = px; y = py; };//这个x,y是在全局变量中定义的
        this.showPoint = function() { alert("x=\t" + x + "\ny=\t" + y); };
    };Point.call(this); 这句其实就是继承Point了,就是在var ColorPoint = function(){}后加上ColorPoint.prototype = new Point();这句this.x和x的区别还是存在的.
      

  7.   


    Point.call(this)
    意思就是
    执行Point函数 改变Point函数的this指向(默认是window)
      

  8.   

    不使用 var 声明变量将会导致隐式的全局变量产生。
      

  9.   

    谢谢 上面各位了!
      现在已把 《javaScript 王者归来》 看完了。
    似懂非懂的,
      想问问 下面的继承的call的返回值是什么?
    function Class3()
    {
    this.alertText= function(txt)
    {
    alert(txt);
    }
    }
    function Class5()
    {
    this.showText= function(txt)
    {
    document.write(txt);
    }
    }
    function Class4()
    {
    Class3.call(this); //本身继承class3 //这个call 方法,它会返回什么给我?
    Class5.call(this); //本身继承class5//这个call 方法,它会返回什么给我?
    }
    var cl4 = new Class4();
    cl4.showText("哈哈");  //
    cl4.alertText("哈哈"); // ///谁能 详细解释一下啊.   
      

  10.   

    函数不一定要有返回值的,call就没有返回值。楼主可以用typeof测试一下,返回undefined。
      

  11.   

    不太同意。call不会改变Point的this指向,this原来也不是指向window,构造函数本身的this都是指向对象本身,而不是window。
      

  12.   

    请问:
     这个call 的内部是怎么实现的? 我们可不可以重写 call 函数啊?---是想看看call 里面的代码!
      

  13.   


    没有实例化的时候 都是指向window的 并不是“构造函数本身的this都是指向对象本身”“call不会改变Point的this指向” 我说的改变只是形象的说一下 可以说是伪造
      

  14.   

    var Word = function (d){
       this.data =d?d:"字";
    };
    var Vocable = function(d){
       this.datas =d?d:"词语";
       Word.call(this,"A");//<1>这句,请看我在下面的解释。
    }; 
     var ve = new ("新年快乐!!"); //<2>
    我的解释: //请你说说下面的理解对不对!
    <1>:这句话的意思是:
      调用 函数Word 但在 函数Word内部的this 这个针指,让它指向Vocable 所实例化的对象
    (如果,Vocable 的构造函数是 语句<2> 引发的,那么 Word内部的this针指 指向语句<2>的变量 ve )
      

  15.   

    function a(){
      this.a = "123"
    }function b(){
      a.call(this) //执行a,伪造a的this指向当前对象
    //以下就可以访问a内部的东西了}
    alert((new b()).a) //123
      

  16.   

    上面的,各位,谢谢了,现在总算明白,call 是怎么个回事,
      还有一个问题,大家 都把 call apply 这类继承叫做“构造继承”。
     听说 构造继承 比 复制继承 快,
      这个,不太明的。
    按我的理解是:几乎一样快,因为他们一个编历一遍父类,一个执行一次父类, 都是一遍啊!
    复制继承 :(下面是我自已写的,也不知对不对。)
      var Word = function (d){
       this.data =d?d:"字";
    };
    var Vocable = function(d){
       this.datas =d?d:"词语";
       for(var key in Word )
     var ve = new Vocable("新年快乐!!"); //<2>
       {
          this[key] = Word[key];
       }
    }; 
      
      

  17.   

    上面的发错了。
    var Word = function (d){ 
       this.data =d?d:"字"; 
    }; 
    var Vocable = function(d){
      this.datas =d?d:"词语";
      for(var key in Word ) 
      {
        this[key] = Word[key];
      }
    };
    var ve = new Vocable("新年快乐!!"); //<2>
      

  18.   

    var ColorPoint = function(){    this.color = "#FFFFFF";
        Point.call(this); //this 指的是Obj
    };你的这个其实就是定义一个类,类似java中的类。里面的this指向当前的对象,而不是window。其实你的这段代码如果这样写会更清楚一些
    function ColorPoint (){    this.color = "#FFFFFF";
        Point.call(this); //this 指的是Obj
    };这两个是一段等同的代码。