Some Help Understanding Window Object [duplicate]

久未见 提交于 2019-12-20 07:56:39

问题


Possible Duplicate:
JS Window Global Object

Can some one please help me understand how the window object works?

I know that it is the top level object there is and that the window object represents an open window in a browser.

Can some one please help me understand more about it; maybe a link or small explanation about the window object? I know that it has all object properties and methods. I have bean told it can recreate by calling the window.constructor and get its native code, also that's the only way to get an instance of the object.

I'll be happy if some one will help me get more info about it.


回答1:


The window object is effectively two things:

  1. The global object for browser-based JavaScript. All of the native objects and methods (Array, String, setTimeout()) and anything you declare outside of any function's scope goes in the window object. To test this, try opening a JavaScript console and checking this out:

    window.String === String
    // Returns true
    
  2. The window object also deals with the browser window. window.innerWidth is the window's width; window.onresize is a function that is triggered on the window resize. Because it's the "topmost" object, you can also say things like innerWidth to get the window's width.

In general, it's a good practice to refer to write window.location instead of just location. Even though they'll work a lot of the time, you'll sometimes run into situations like this (which you don't want!):

function something() {
    var location = 'the moon';
    location.reload();    // Should be window.location.reload()
}

In the above example, you might've meant to refresh window.location instead of the location scoped inside this function.

And that's the window object!



来源:https://stackoverflow.com/questions/10037145/some-help-understanding-window-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!