window.onload = function(){
                var hatImage = document.getElementById('hatImage');
                var button = document.getElementById('changeImage');
                
                
                this.showRabbit = function(){
                    hatImage.setAttribute("src", "rabbit-hat.gif");
                    button.setAttribute("value", "Hide Rabbit");
                    button.onclick = hideRabbit;
                }
                
                this.hideRabbit = function(){
                    hatImage.setAttribute("src", "topHat.gif");
                    button.setAttribute("value", "Add a rabbit");
                    button.onclick = showRabbit;
                }
            }
加上this在外面就能调用了...  为什么??? 为什么用var showRabbit = fcuntion(){}在外面就调不到.
还有, ,我一直不懂这个 this代表什么? window吗.? 还是document ,高手教我

解决方案 »

  1.   

    <BODY id='test' onload="alert(this.document.body.id);">
        <input type='button' onclick="alert(this.tagName);">
    </BODY>
      

  2.   

    给window的load事件绑定了一个匿名函数.那么当load事件发生的时候,调用这个匿名函数的是window,所以内部的this指向的就是window
      

  3.   

    加上this,实际就是定义一个变量window.a,它的值是一个函数。如果不加this,用var定义,那么它就是匿名函数的私有变量,外部自然不能访问。如果不加this ,也不用var定义,那么默认这个变量是属于window的,是一个全局变量,因此外部同样可以访问
      

  4.   

    加上this后,外面也不能够访问的!
                window.onload = function(){
                     this.a="123";
                }
                alert(a); //报a未定义的错误!
                alert(window.a); // 警告框“undefined”
    this加方法 结果类似!
    以上结果刚刚在IE8、Chorme、FireFox测试。
      

  5.   


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    <script type="text/javascript">
    window.onload = function() {
    this.a = function() {
    alert('a');
    }
    b = function() {
    alert('b');
    }
    }
    </script>
    </head>
    <body>
    <script type="text/javascript">
    try{
    a();
    b();
    } catch(e) {
    setTimeout(function(){a();b()}, 1000);
    }
    </script>
    </body>
    </html>
      

  6.   

    楼主你说的是javascript面向对象编程的。其中如果需要给js类增加属性(包括方法)是有两种方式的:
    1.this."<property or method>"的方式实现
    2.classFunction.prototype.[FunctionName]
    如果你想外部调用(是不需要var定义的)
    function Test()
    {
       function start(){};
    }var test=new Test();
    test.start();
      

  7.   

    确实要延时后才可以调用 setTimeout()