如何在JavaScript中获取全局对象? [英] How to get the global object in JavaScript?

查看:146
本文介绍了如何在JavaScript中获取全局对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查脚本是否已加载某个其他模块。

I want to check in a script if a certain other module is already loaded.

if (ModuleName) {
    // extend this module
}

但是如果 ModuleName 不存在,抛出 s。

But if ModuleName doesn't exist, that throws.

如果我知道全局对象我可以使用它。

If I knew what the Global Object was I could use that.

if (window.ModuleName) {
    // extend this module
}

但是因为我希望我的模块适用于两种浏览器和节点 rhino 等,我不能假设窗口

But since I want my module to work with both browsers and node, rhino, etc., I can't assume window.

根据我的理解,这在ES 5中不起作用use strict;

As I understand it, this doesn't work in ES 5 with "use strict";

var MyGLOBAL = (function () {return this;}()); // MyGlobal becomes null

这也会因抛出异常而失败

This will also fail with a thrown exception

var MyGLOBAL = window || GLOBAL

所以好像我已经离开了

try {
    // Extend ModuleName
} 
catch(ignore) {
}

这些案例都不会传递JSLint。

None of these cases will pass JSLint.

我错过了什么?

推荐答案

那么,您可以使用 typeof 运算符,如果是标识符在范围链的任何地方都不存在,抛出 ReferenceError ,它只会返回 undefined

Well, you can use the typeof operator, and if the identifier doesn't exist in any place of the scope chain, it will not throw a ReferenceError, it will just return "undefined":

if (typeof ModuleName != 'undefined') {
  //...
}

还要记住 this 全局代码的值,指的是全局对象,这意味着如果你的 if 语句在全局上下文中,你只需检查 this.ModuleName

Remember also that the this value on Global code, refers to the global object, meaning that if your if statement is on the global context, you can simply check this.ModuleName.

关于(function(){return this;}()); 技术,你是对,在严格模式下,值将只是 undefined

About the (function () { return this; }()); technique, you are right, on strict mode the this value will simply be undefined.

在严格模式下,有两种方法可以获得对Global对象的引用,无论您身在何处:

Under strict mode there are two ways to get a reference to the Global object, no matter where you are:


  • 通过函数构造函数:

var global = Function('return this')();


使用函数构造函数不继承调用者的严格性,只有当他们使用'use strict'指令启动它们的主体时它们才是严格的,否则它们是非严格的。

Functions created with the Function constructor don't inherit the strictness of the caller, they are strict only if they start their body with the 'use strict' directive, otherwise they are non-strict.

此方法与任何ES3实现兼容。

This method is compatible with any ES3 implementation.


  • 通过间接 eval 致电,例如:

  • Through an indirect eval call, for example:

"use strict";
var get = eval;
var global = get("this");


以上是有效的,因为在ES5中,间接的调用 eval ,使用全局环境同时作为评估代码的可变环境和词汇环境。

The above will work because in ES5, indirect calls to eval, use the global environment as both, the variable environment and lexical environment for the eval code.

详见输入评估代码,第1步。

但要注意最后的解决方案不适用于ES3实现,因为在ES3上间接调用 eval 将使用调用者的变量和词法环境作为eval代码本身的环境。

But be aware that the last solution will not work on ES3 implementations, because an indirect call to eval on ES3 will use the variable and lexical environments of the caller as the environments for the eval code itself.

最后,您可能会发现有助于检测是否支持严格模式:

And at last, you may find useful to detect if strict mode is supported:

var isStrictSupported = (function () { "use strict"; return !this; })();

这篇关于如何在JavaScript中获取全局对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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