The window object is a global object, which means you don’t need to use its name to access its properties and methods. In fact, the global functions and variables (the ones accessible to script anywhere in a page) are all created as properties of the global object. For example, the alert() function you have been using since the beginning of the book is, in fact, the alert() method of the window object. Although you have been using this simply as this:alert(“Hello!”);You could write this with the same, exact results:
window.alert(“Hello!”);However, since the window object is the global object, it is perfectly correct to use the first version.window是全局对象,调用它的方法和属性就不用写window.了,怎么理解?
其他面向对象的语言可不是这样的。JavaScript对象

解决方案 »

  1.   

    (function(){
    var alert = function(msg){ window.alert('kk:'+msg) }alert(1);
    window.alert(1);
    })();这样能明白吧 js有查找顺序的
      

  2.   

    难道我写的js代码都是在window这个类里面?
      

  3.   

    js中有个global对象,是隐藏的,一切全局的都是他的属性,也即是window的属性
    可以这样
    global.pro;
    不过全局的global要省略
      

  4.   

    内容来自javascript高级程序设计第二版
      

  5.   

     js 就是规定了
    默认情况会自动在 window下查找
    所以写很短小的代码的时候
    大家都省略了 window 反正系统会帮你找的但是好的习惯 你应该写  window.alert 显式调用
    很简单的规则 了解就可以了
      

  6.   


    是这个意思吧:
    This happens because any function or variable
    you defi ne within the global scope actually gets appended to the window object. Look at this code
    as an example:
    var myVariable = “Hello, World!”;
    alert(window.myVariable);
    If you were to execute this code in a browser, the alert window will display the message “Hello, World.”
      

  7.   

    <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
    “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
    <html xmlns=”http://www.w3.org/1999/xhtml”>
    <head>
    <title>Chapter 6: Example 1</title>
      <script type=”text/javaScript”>
      window.defaultStatus = “Hello and Welcome”;
      </script>
    </head>
    <body>
    </body>
    </html>为什么这copy的这段代码在firefox上不起作用呢?
    这个Hello and Welcome显示在哪里?
      

  8.   

    <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
    “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
    <html xmlns=”http://www.w3.org/1999/xhtml”>
    <head>
    <title>Chapter 6: Example 1</title>
      <script type=”text/javaScript”>
      window.defaultStatus = “Hello and Welcome”;
      for(var i in window) {
          if(i == defaultStatus) alert(window[i]);
      }
      </script>
    </head>
    <body>
    </body>
    </html>
      

  9.   


    老是提醒这个错误:时间戳: 2013/5/22 16:26:48
    错误: HTML 文档的字符编码未声明。如果该文件包含 US-ASCII 范围之外的字符,该文件将在某些浏览浏览器配置中呈现为乱码。页面的字符编码必须在文档或传输协议层声明。
    源文件:file:///G:/JavaScript/try.htm
    行:0
      

  10.   


    是可以这样理解,js的一切都在一个map(object)里。
      

  11.   

    <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Strict//EN[color=#FF0000]”[/color]
    “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
    <html xmlns=http://www.w3.org/1999/xhtml>
    <head>
    <title>Chapter 6: Example 1</title>
      <script type=text/javaScript>
      window.defaultStatus = “Hello and Welcome”;
      </script>
    </head>
    <body>
    </body>
    </html>你不觉得这些引号看起来怪怪的吗
      

  12.   


    说白了就相当于在整个脚本外面加个with(window){}
      

  13.   


    嗯 我发现了  但是改了以后还是这个错误 defaultStatus不能显示出来
      

  14.   


    说白了就相当于在整个脚本外面加个with(window){}
    呵呵 我还没学with呢
      

  15.   


    嗯 我发现了  但是改了以后还是这个错误 defaultStatus不能显示出来
    你只是给它赋值,当然不会显示了。
    window.defaultStatus = "Hello and Welcome";
      alert(defaultStatus);
      

  16.   


    嗯 我发现了  但是改了以后还是这个错误 defaultStatus不能显示出来
    你只是给它赋值,当然不会显示了。
    window.defaultStatus = "Hello and Welcome";
      alert(defaultStatus);我不是要它弹出来,书上说可以在浏览器状态栏显示出来:To change the default message in the window’s status bar, you need to use the window object’s defaultStatus property. To do this, you can write the following:
    window.defaultStatus = “Hello and Welcome”;