简体   繁体   English

typeof window ==='object'`始终为真吗?

[英]Will `typeof window === 'object'` always be true?

Assuming window isn't shadowed, will typeof window === 'object' always be true? 假设窗口没有被遮挡, typeof window === 'object'始终为true吗? Is this safe? 这样安全吗?

Context: I'm trying to export a module to the global scope using browserify and I want to include a check to see if this module is run in the browser and if it is, I want to export it to the global object. 上下文:我正在尝试使用browserify将模块导出到全局范围,并且我希望包括检查该模块是否在浏览器中运行以及是否要导出到全局对象。

If you have the real window , defined by the runtime, it will always be an object. 如果您具有运行时定义的实际window ,它将始终是一个对象。 The typeof operator (§12.5.6) has a limited number of return values (table 15) , and there's no special one for window . typeof运算符(第12.5.6节)返回值数量有限(表15) ,并且window没有特殊的返回值 If they expand that table, it could change, but the only applicable result in ES1 through ES7 is 'object' . 如果他们扩展该表,它可能会更改,但是从ES1到ES7唯一适用的结果是'object'

Bear in mind that Node doesn't define window at all (so typeof would be 'undefined' ) and you can shadow it, as you mention. 请记住,Node根本没有定义window (因此typeof将是'undefined' ),您可以将其阴影化,正如您提到的那样。 In a browser with a standard DOM implementation, window will be the object defined in §7.3 of the DOM specification . 在具有标准DOM实现的浏览器中, window将是DOM规范的 §7.3中定义的对象。 For JavaScript, this should be an object and I believe all browsers expose it as such. 对于JavaScript,这应该是一个对象,我相信所有浏览器都将其公开。

In webpack, you would use the output.library property and set output.libraryTarget to 'var' , then the bundle sets up the global for you. 在webpack中,您将使用output.library属性并将output.libraryTarget设置为'var' ,然后捆绑包将为您设置全局变量。 I imagine browserify has an equivalent. 我认为browserify具有同等功能。

The answer is: It depends. 答案是:这取决于。

Short answer: 简短答案:

If you're in a browser, then yes, window will always be an object. 如果您使用的是浏览器,则可以, window将始终是一个对象。 If you're on a JavaScript standalone engine such as Node.js, then probably no. 如果您使用的是JavaScript独立引擎(例如Node.js),则可能不会。

More details: 更多细节:

A window is one of the top-hierarchy objects on a browser. 窗口是浏览器上的顶级对象之一。 It is the parent of the whole DOM tree present inside a browser window (or tab), and the context reference where most of functions and event listeners will often run. 它是浏览器窗口(或选项卡)中存在的整个DOM树的父级,并且是大多数功能和事件侦听器经常运行的上下文引用。

JavaScript对象层次结构

So in any browser that expects to work properly, there will be a window object. 因此,在任何期望正常运行的浏览器中,都会有一个window对象。 (some side/experimental projects may have modified JavaScript engines and not implement window , but this is highly unlikely and definitely not the kind of thing that is going to be used by "mainstream users") (某些辅助/实验项目可能已修改了JavaScript引擎,但未实现window ,但这极不可能,而且绝对不是“主流用户”将要使用的那种东西)

However, when we talk about JavaScript engines running outside browsers, things get a little different. 但是,当我们谈论运行在浏览器外部的JavaScript引擎时,情况会有所不同。 On a server-side JavaScript runtime such as Node.js, the purpose is not to work with windows/tabs, URLs and DOM tree parsing such as a browser do. 在服务器端JavaScript运行时(例如Node.js)上,其目的不是与窗口/选项卡,URL和DOM树解析(例如浏览器)一起使用。 So you will not have this object available and running typeof window will return 'undefined' on those environments. 因此,您将没有可用的该对象,并且在这些环境中运行的typeof window将返回'undefined'

To add a variable to global scope, browserify exposes global which in the browser represents window . 要将变量添加到全局范围,browserify公开global ,该值在浏览器中表示window

Per the browserify docs (emphasis mine) 根据browserify文档 (重点是我的)

Additionally, if you use any of these variables, they will be defined in the bundled output in a browser-appropriate way: 此外,如果您使用以下任何变量,则将在捆绑输出中以适合浏览器的方式定义它们:

  • process
  • Buffer
  • global - top-level scope object (window) global -顶级范围对象(窗口)
  • __filename - file path of the currently executing file __filename当前正在执行的文件的文件路径
  • __dirname - directory path of the currently executing file __dirname当前执行文件的目录路径

As far as the question being asked: 就所提问题而言:

Assuming window isn't shadowed, will typeof window === 'object' always be true? 假设窗口没有被遮挡,typeof window ==='object'始终为true吗?

Yes given that it's running in a browser environment. 是的,因为它运行在浏览器环境中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM