在严格模式下获取未知环境中的全局对象的引用 [英] Getting a reference to the global object in an unknown environment in strict mode

查看:257
本文介绍了在严格模式下获取未知环境中的全局对象的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ES5严格模式下获取全局对象句柄的推荐方法是什么? >在未知的主机环境中?

What is the recommended way to get a handle to the global object in ES5 strict mode in an unknown host environment?

ECMAScript没有提供引用我所知道的全局对象的内置方法。如果是这样,这就是我正在寻找的答案。

ECMAScript doesn't provide a built-in way to reference the global object that I'm aware of. If it does, this is the answer I'm looking for.

已知环境中,全局对象通常具有自我指涉属性。由于全局对象是全局范围的 VO ,因此全局对象的属性是全局变量,所以我们可以使用它们从任何地方获取全局对象的句柄:

In a known environment, the global object usually has a self-referential property. Since the global object is the VO for the global scope, properties of the global object are global variables, so we can use them get a handle to the global object from anywhere:


  • 在Web浏览器中,我们可以使用 window self

在node.js中,我们可以使用 global

In node.js, we can use global.

但是,并非所有主机环境都是如此。据我所知,Windows Script Host不提供任何访问全局对象的方法。在WSH中获取全局对象的推荐方法似乎是在不解析为对象的上下文中使用 this 关键字。例如:

However, this is not necessarily the case in all host environments. As far as I know, Windows Script Host does not provide any way to access the global object. The recommended way to get the global object in WSH seems to be to use the this keyword in a context where it does not resolve to an object. For example:

var GLOBAL = (function(){return this}());

此技术适用于任何主机环境,但不适用于严格模式,因为未定义的未在严格模式中引用全局对象:

This technique will work for any host environment, but not in strict mode, because an undefined this does not reference the global object in strict mode:


如果在严格模式代码中进行评估,则不会将此值强制转换为对象。 此null或undefined的值不会转换为全局对象,并且原始值不会转换为包装器对象。通过函数调用传递的此值(包括使用Function.prototype.apply和Function.prototype.call进行的调用)不会强制将此值传递给对象(10.4.3,11.1.1,15.3.4.3,15.3。 4.4)。

If this is evaluated within strict mode code, then the this value is not coerced to an object. A this value of null or undefined is not converted to the global object and primitive values are not converted to wrapper objects. The this value passed via a function call (including calls made using Function.prototype.apply and Function.prototype.call) do not coerce the passed this value to an object (10.4.3, 11.1.1, 15.3.4.3, 15.3.4.4).

正如预期的那样,以下代码导致 undefined

As expected, the following code results in undefined:

(function(){
    "use strict";
    var GLOBAL = (function(){return this}());
    console.log(GLOBAL);
}());






那么,什么是在任何环境中获取全局对象的句柄,无论严格模式?

顺便说一句,我目前的方法是嗅探引用全局对象的全局变量,如下所示:

By the way, my current approach is to sniff for global variables referencing the global object like this:

var self, window, global = global || window || self;

...然后只需使用全球 。我认为这是一个糟糕的解决方案,原因有很多,其中大多数是相当明显的,并没有解决WSH问题。

...and then just use global. I think this is a bad solution for a number of reasons, most of which are fairly obvious, and it doesn't address the WSH problem.

推荐答案

在ES5中,您可以通过间接eval调用从严格模式中获取对全局对象的引用:

In ES5, you can get a reference to global object from within strict mode via indirect eval call:

"use strict";
var global = (1,eval)('this');

看看我的文章;特别是在关于严格模式的部分

这篇关于在严格模式下获取未知环境中的全局对象的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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