在ECMAScript 6中全局范围内的`this` [英] `this` in global scope in ECMAScript 6

查看:122
本文介绍了在ECMAScript 6中全局范围内的`this`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试过自己查看ES6草稿,但我不确定在哪里看:

I've tried looking in the ES6 draft myself, but I'm not sure where to look:

有人能告诉我是否 ES6中的这个必然是指全局对象?此外,此对象是否与全局范围具有相同的成员?

Can someone tell me if this in ES6 necessarily refers to the global object? Also, will this object have same members as the global scope?

如果您可以回答ES5也会有所帮助。

If you could answer for ES5 that would be helpful as well.

我知道全局范围内的此是指浏览器和大多数其他ES环境中的全局对象,如Node。我只是想知道这是规范定义的行为,还是实现者已添加的扩展行为(如果这种行为在ES6实现中继续)。另外,全局对象总是与全局范围相同吗?或者有区别吗?

I know this in global scope refers to the global object in the browser and in most other ES environments, like Node. I just want to know if that's the defined behavior by the spec or if that's extended behavior that implementers have added (and if this behavior will continue in ES6 implementations). In addition, is the global object always the same thing as the global scope? Or are there distinctions?

更新 - 为什么我想知道:我基本上都在尝试找出如何在ES5&中可靠地获得全局对象6.我不能依赖窗口,因为那是特定于浏览器的,我也不能依赖全球,因为那是特定于Node等环境。我知道Node中的这个可以在模块范围内引用模块,但我认为它仍然指的是<$ c $全局范围内的c> global 。我想要一个跨环境的ES5&获得全局对象的6种兼容方式(如果可能)。似乎在我所知道的所有环境中,这个在全球范围内做到了这一点,但我想知道它是否是实际规范的一部分(并且在任何环境中如此可靠)我可能不熟悉)。

Update - Why I want to know: I am basically trying to figure out how to get the global object reliably in ES5 & 6. I can't rely on window because that's specific to the browser, nor can I rely on global because that's specific to environments like Node. I know this in Node can refer to module in module scope, but I think it still refers to global in global scope. I want a cross-environment ES5 & 6 compliant way to get the global object (if possible). It seems like in all the environments I know of this in global scope does that, but I want to know if it's part of the actual spec (and so reliable across any environment that I may not be familiar with).

我还需要知道全局范围和全局对象是否与规范相同。换句话说,全局范围内的所有变量都与 globalobject.variable_name 相同?

I also need to know if the global scope and the global object are the same thing by the spec. In other words will all variables in global scope be the same as globalobject.variable_name?

更新2 - 我正在尝试做什么:

我已经开发了一些针对ES5环境的ES6填充程序。我想知道(1)检查ES6内置函数是否已经存在以便在可能的情况下使用它们而不是我的垫片,以及(2)将垫片添加到全局范围内的最佳方法(1) ins还不存在。

I have developed some ES6 shims for ES5 environments. I want to know the best way to (1) check to see if the ES6 built-ins already exist so that they can be used when possible instead of my shims, and (2) add my shims to the global scope if the built-ins do not already exist.

目前我正在遵循这种模式:

Currently I'm following this pattern:

(function() {

    // Indirect eval to run in global scope.
    // (We get whatever "this" is in global scope, hoping that it's the global object...
    // Whether this line does what I want it to is the crux of my question.)
    var global = (0, eval)('this');

    // If Symbol does not already exist in global scope,
    if (!global.Symbol)

        // Then add Symbol to global scope.
        global.Symbol = (function() {

            // ...
            // Return my Symbol shim

        })();

})();

(1)还有其他一些可能性,但在一天结束时我需要一种方式在不使用全局范围内的 var 的情况下向全局范围添加内容(因为在我检查它们之前会覆盖内置函数,因为 var 吊装[至少在天真的情况下,也许我可以间接 eval 一个 var 声明还有?])。我希望我的代码能够以严格的模式运行,这样才能解决问题。

There are some other possibilities for (1), but at the end of the day I need a way to add something to global scope without using var in global scope (because that would override the built-ins before I can check for them, due to var hoisting [at least in the naive case, perhaps I could indirect eval a var statement as well?]). I want my code to be able to run in strict mode, so that compounds the problem.

我发现,根据ES5规范,间接 eval 在全局范围内执行代码。所以我至少能够做到这一点。我的问题是,如果我在全局范围内得到这个,(1)检查该对象的属性是否让我知道全局范围内是否已存在内置? (2)向该对象添加属性是否允许我将变量添加到全局范围?

I have discovered that, by the ES5 spec, indirect eval executes code in global scope. So I am at least able to do that. My questions are if I get this in global scope, (1) Will checking the properties of that object let me know if a built-in already exists in global scope? and (2) Will adding properties to that object allow me to add variables to global scope?

推荐答案

是,全局范围内的此将继续引用ES6中的全局对象。 (通常,ES6应该完全向后兼容,即任何保证在ES5中工作的代码也应该在ES6中工作)。

Yes, this in global scope will continue to refer to the global object in ES6. (Generally, ES6 is supposed to be fully backwards compatible, i.e. any code that was guaranteed to work in ES5 should also work in ES6).

全局范围的概念但是,将不再与ES6中的全局对象相同。它引入了词法范围的新声明表单( const class 模块,还有一些)。上次会议的结论是,这些都不会作为全球对象的属性出现。这有多种技术和方法上的原因,但最重要的是最好避免直接使用全局对象(这一直是正确的,但在ES6中更是如此)。

The notion of "global scope", however, will no longer be identical with the global object in ES6. It introduces new declaration forms that are lexically scoped (let, const, class, module, and a few more). The conclusion at the last meeting was that none of these will appear as properties of the global object. There is a variety of technical and methodological reasons for that, but the bottom line is that it is best to avoid using the global object directly altogether (this has always been true, but is even more so in ES6).

您是否需要特定的全局对象?

Is there something specific you need the global object for?

这篇关于在ECMAScript 6中全局范围内的`this`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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