这在全球范围内处于严格模式(在ES2015中) [英] this in a strict mode in a global scope (in ES2015)

查看:101
本文介绍了这在全球范围内处于严格模式(在ES2015中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以指出ES2015标准中的那个地方,说明在全球范围内以严格模式使用时,这个必须引用哪些

我发现它等于我的firefox和chrome中的窗口,但不等于窗口在我的IE11在Windows 7 x64。



我可以找到的标准中唯一相关的地方是 ES2015 - ECMAScript的严格模式,但它看起来模糊不清没有明确说明。

解决方案

ECMAScript规范没有定义哪个对象应该是特定运行时环境中的全局。对于浏览器,这是由 HTML5规范定义的。通过 windowproxy,有一些专门的窗口 对象,但通常应该是javascript不可见的。



即使浏览器脚本中的顶级 this!== window 也不一定源于ecma spec属性或严格模式。它也可能源于html规范的定义,该特定脚本环境应该是全局应用程序,还是源自全局实际上等于<局部变量绑定> .window 。 / p>




您的问题是您所说的全球范围。该规范具有全球词汇环境的概念。但并不是所有的脚本体(源代码)都是用领域的全球词汇环境作为其词汇环境进行评估。



另外一个复杂的问题是,脚本不能仅仅运行在不同的词汇环境,但也可以有多个领域,它们可以进行交互,但具有全局性。

这可能看起来像一个非问题,因为不同的代码领域(例如工作人员或浏览器窗口)主要通过小型API表面进行交互,但现实比这更复杂。例如。一个领域可以使用全局对象,该对象具有作为不同领域全局的代理的原型。在许多方面,他们看起来像共享一个全球,当他们实际上不是,并做一些像这个== this.referenceToSelf 可能会返回false。这通常发生在Firefox的沙箱用于运行addons / webextensions / userscripts。






一个顶级这个不是全局对象将是es6模块。





所以存在严格的模式代码,其中这个在顶级词汇不是全局对象。



一个比较简单的例子是在函数上下文中的代码eval。它从该函数继承这个


Could someone please point to the place in the ES2015 standard that states what the this must refer to when used in a strict mode in a global scope?

I have found that it is equal to the window in my firefox and chrome, but is not equal to window in my IE11 under windows 7 x64.

The only relevant place in the standard I could find is ES2015 - The Strict Mode of ECMAScript but it looks vague and does not state that explicitly.

解决方案

The ECMAScript spec does not define which object should be the global in particular runtime environments. For browsers this is defined by the HTML5 spec. There is some special-casing for window via the windowproxy object, but that should generally be invisible to javascript.

I.e. even if top level this !== window in a browser script this does not necessarily stem from ecma spec properties or strict mode. It could also stem from from the html spec's definition what a global ought to be for that particular script environment or whether that global actually equals <local variable bindings>.window.


An issue with your question is what you call the global scope. The spec has a concept of global lexical environment. But not all script bodies (source code) are evaluated with a realm's global lexical environment as their lexical environment.

A further complication is that that scripts can not just run in different lexical environments but that there can also be multiple realms which can interact and yet have distict globals.
This may seem like a non-issue because different code realms (e.g. workers or browser windows) mostly interact through small API surfaces, but reality is more complex than that. E.g. one realm can use a global object which has a prototype which is a proxy to a different realm's global. In many ways they will look like sharing a global when they actually are not and doing something like this == this.referenceToSelf may return false. This commonly happens with firefox's sandboxes which are used to run addons/webextensions/userscripts.


A counterexample where a "top level" this is not the global object would be es6 modules.

So there exists strict mode code where this in the top level lexical is not the global object.

A much more trivial example is code eval'd in the context of a function. It inherits the this from the function.

这篇关于这在全球范围内处于严格模式(在ES2015中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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