JS的全局变量是不是属于widow的变量?如果是,那为什么“this == document”和“this == frames”都可以打印出来?如果因为document和frames是window子对象所以可以打印,为什么同样是子对象的anchors,forms,images,navigator和screen却不能打印?
请高手指点,非常感谢!!
<html>
<head>
</head>
<body>
<script language="JavaScript" type="text/JavaScript">
if (this == document) alert("document"); //document
if (this == window) alert("window"); //window
if (this == frames) alert("frames"); //frames
                        //if (this == anchors) alert("anchors");
//if (this == forms) alert("forms");
//if (this == images) alert("images");
//if (this == navigator) alert("navigator");
//if (this == screen) alert("screen");
if (this === window) alert("window"); //window
</script>
</body>
</html>javascript  JS  this

解决方案 »

  1.   

    二楼xiaofanku的图片网上很容易找到,而且二楼没有回答我的问题
      

  2.   

    this一般在对象中使用,您的意思我不太明白.
      

  3.   

    this应该是指向一个特定的对象吧,那为什么判断指向document,window和frames三个不同的对象都是对的?
      

  4.   

    其实#1的图还不错,我是第一次见。楼主要问的是为什么有的alert能弹出,有的alert不能弹出
      

  5.   

    javascript中的this不像是.net中的this。javascript里涉及到作用域的问题,this是挺复杂的,经验来讲在javascript中的this一般是谁调用就是谁。所以建议你看看this的有关文章,有助于你更好的理解。
      

  6.   

    this会根据代码的上下文环境而改变。楼主你这种alert想搞懂什么?
      

  7.   

    为什么同样是子对象的anchors,forms,images,navigator和screen却不能打印?
    re:
    谁告诉anchors,forms,images,navigator和screen是window的直接子对象。如果你指的是忽略继承层次。
      

  8.   

    就是为了试验this而已
    前辈你那图我刚看明白
    请不要把菜鸟的理解能力想得那么高其实那图已经回答了关键点
      

  9.   

    http://www.laruence.com/2009/09/08/1076.html
    我觉的解释挺到位的,可以参考下。
      

  10.   

    好吧. 实际上你用不同浏览器会发现: ie支持this==document. 但firefox不支持this==document. 
    而window与frames实际是一个引用所以不要纠结为什么this可以指向document,也可以指向window.不同内核的机制不一样.一般来说,this指向window. 而DOM里的东西严格按照标准取法来取.一般来说this会广泛的使用于类中,这里的用法网上很多.与OO思想也是基本一致的