20毫秒以后MyObject对象已经不存在了…………
this指向谁呢?在setTimeout和setInterval里面都不可以用这个

解决方案 »

  1.   

    var obj = document...........
    obj.a = ...
    obj.fn = ...
    obj.OnTimer = ...setTimeout( "obj.OnTimer()", 20 );
      

  2.   

    但MyObject对象是全局的,所以不应该不存在啊。// 在构造函数中使用this执行了
    // setTimeout()
    MyObject obj( .. );
      

  3.   

    try:function MyObject( ... )
    {
        this.a = ...
        this.fn = ...
        this.OnTimer = ...
        // ...
        setTimeout( "eval(this.OnTimer())", 20 );
    }
      

  4.   

    setTimeout( "this.OnTimer()", 20 );
    系统肯定不会认为this是你创建的对象,什么是this?
    setTimeout不是我们定义的对象的方法或属性,所以。。
    出现这种情况是正常的。
      

  5.   

    setTimeout是系统的方法,但OnTimer是this的方法,所以有点奇怪。
      

  6.   

    setTimeout 里调用的方法都是 window 下的方法或者 window 某个属性下的方法.
    setTimeout("method()", 10)  等效于 setTimeout("window.method()", 10)
    所以你在它里用this就变味了 setTimeout( "window.this.OnTimer()", 20 );
    这里明显就不对了, 所以我在写JS类的时候都这样处理一下:
    function MzPopupMenu(Tname)
    {
      if(typeof(Tname) != "string" || Tname == "")
        throw(new Error(-1, 'Please input MzMenu instance name'));  setTimeout( Tname +".OnTimer()", 20 );
      //....
    }
    var obj = new MyObject("obj");
      

  7.   

    setTimeout( "this.OnTimer()", 20 );不能放在构造器里吧
      

  8.   

    function Test(){
     this.value=1;
     this.n=1;
     this.foo=foo;
    }
    function foo(){
     this.n+=this.value;
     window.setTimeout("this.foo()",100);
    }
    (new Test()).foo();
    这样可以。
      

  9.   

    To: wwser(我要这天,再也遮不住我眼,我要这地,再也埋不了我心)    你这种写法不等同于直接在结构里定义的方法:
    function foo()
    {
      this.n+=this.value;
      alert(this.n);
      window.setTimeout("this.foo()",1000);
    }
    比如说我 alert(this.n), 由于第一次调用的是 new Test().foo(), 弹出的值是对, 但是在后面弹出的值就不对了. 因为你以后调用的 foo() 已经不是new Test()这个类里的方法而是直接调用 window.foo() 这个函数了.
      

  10.   

    看着,这是正确方法<script>
    function Test(){
     this.value=1;
     this.n=1;
     this.foo=foo;
    }
    function foo(){
     var self = this;
     this.n+=this.value;
     alert(this.n);
     f = function(){ self.foo() }
     window.setTimeout(f,1000);
    }
    (new Test()).foo()
    </script>
      

  11.   

    能够这样嘛,在构造函数中调用。
    <script>
    function foo(){
     this.n+=this.value;
     //alert(this.n);
    }
    function Test(){
     this.value=1;
     this.n=1;
     this.foo=foo;
     f = function(){ this.foo() }
     window.setInterval(f,1000);}(new Test()).foo()
    </script>
      

  12.   

    这个问题阿信的用 另一个变量 self 代替 this 不是已经给出解决之路了吗??
    <script>
    function foo(){
     this.n+=this.value;
     alert(this.n);
    }
    function Test(){
     this.value=1;
     this.n=1;
     this.foo=foo;
     var me = this;
     f = function(){me.foo() }
     window.setInterval(f,1000);}(new Test()).foo()
    </script>
      

  13.   

    谢谢,我试了一下。<html>
    <head>
    <title>
    Hello Timer
    </title>
    <script language = "JScript">function Obj()
    {
    function foo()
    {
    alert( "Hello" );
    }

    this.timer = foo;
    var me = this;
    var f = function() 
              // 必须这样,
             { me.timer(); };
                        // 不能这样 
                         // { this.timer(); };
    setInterval( f, 1000 );
    }var o = null;function OnClick()
    {
    o = new Obj();
    // ok
    //setInterval( "o.timer()", 1000 );
    }</script>
    </head>
    <body>
    <input type = "button" onclick = "OnClick()" value = "Click me"></input>
    </body>
    </html>但不知道为什么要使用一个中间对象。而不能直接使用this.
      

  14.   

    可以这样,封装的好一点。function Obj()
    {
    function setTimer( obj )
    {
    setTimeout( function() { obj.BuildA(); }, obj.timer );
    } function foo()
    {
    alert( "Hello" );
    }
    this.timer = foo;
    /*
    var me = this;
    var f = function() 
              // 必须这样,
             { me.timer(); };
                        // 不能这样 
                         // { this.timer(); };
    setInterval( f, 1000 );
    */
             setTimer( this );
    }var o = null;function OnClick()
    {
    o = new Obj();
    // ok
    //setInterval( "o.timer()", 1000 );
    }
      

  15.   

    在网页里若不指定 this 代表的对象, 那 this 就默认指向 window 对象, 因为网页里所有的对象及属性都是从 window 这个根继承下来的, 所以在 setTimeout 里若使用 this 那这个 this 都指向了 window
      

  16.   

    to meizz 
    <input type='text' name='ice' value='iceberg' onclick='alert(value)'/>像这样的写法this应该是默认为事件的srcElement吧
    这里的value = this.value
      

  17.   

    其实关键在于你写window.setTimeout("this.foo()",1000);这句代码时,this只是一个字符串,而不是具体对象实例的地址的引用,所以在不同的具体执行环境中这个this将会有不同的指向解释window.setTimeout("this.foo()",1000);也就是说setTimeout是window对象的方法,所以执行setTimeout方法的是window这个对象实例,"this"这个字符串将在window这个对象环境中被解释为指向window对象实例,而不是你所想当然的那个对象实例而通过
    var me = this;
    f = function(){me.foo() }
    window.setTimeout(f,1000);
    实际上就是将具体某个对象实例的地址引用传过去了