一些帮助理解窗口对象 [英] Some Help Understanding Window Object

查看:51
本文介绍了一些帮助理解窗口对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
JS 窗口全局对象

有人可以帮我理解 window 对象是如何工作的吗?

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

我知道它是顶级对象,并且 window 对象代表浏览器中打开的窗口.

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

有人可以帮我了解更多吗?也许是关于 window 对象的链接或小解释?我知道它具有所有对象属性和方法.我让 bean 告诉它可以通过调用 window.constructor 重新创建并获取其本机代码,这也是获取对象实例的唯一方法.

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.

推荐答案

window 对象实际上是两件事:

The window object is effectively two things:

  1. 基于浏览器的 JavaScript 的全局对象.所有本机对象和方法(ArrayStringsetTimeout())以及您在任何函数范围之外声明的任何内容都在 <代码>窗口对象.要对此进行测试,请尝试打开 JavaScript 控制台并检查:

  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

  • window 对象也处理浏览器窗口.window.innerWidth 是窗口的宽度;window.onresize 是在窗口调整大小时触发的函数.因为它是最顶层"的对象,所以你也可以使用 innerWidth 之类的东西来获取窗口的宽度.

  • 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.

    一般来说,参考编写window.location 而不仅仅是location 是一种很好的做法.尽管它们会在很多时候工作,但您有时会遇到这样的情况(您不想要!):

    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()
    }
    

    在上面的示例中,您可能想要刷新 window.location 而不是此函数内的 location 范围.

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

    这就是窗口对象!

    这篇关于一些帮助理解窗口对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆