为什么是“这个"?在 <script type="module"> 中未定义? [英] why is "this" undefined in <script type="module">?

查看:58
本文介绍了为什么是“这个"?在 <script type="module"> 中未定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 ECMAScript 模块发现,在类型为 module 的脚本的全局"范围内调用 thisundefined 但在普通脚本中,它引用到 window 对象

Playing around with ECMAScript Modules just found that calling out this in the "global" scope of a script with type module is undefined but in a normal script, it references to the window object

ES 模块脚本

    <script type="module">
      console.log(this); // undefined
    </script>

普通脚本

    <script>
      console.log(this); // window object
    </script>

是否有任何关于此的规范,或者有人可以解释我为什么?

Is there any spec about this, or can someone explain me why?

推荐答案

ES6 模块被指定为 this 值为 undefined.您可以在 8.1 中查看规范定义.1.5.4 模块环境记录:GetThisBinding().

ES6 modules are specced such that their this value is undefined. You can see the spec definition in 8.1.1.5.4 Module Environment Records: GetThisBinding().

8.1.1.5.4 GetThisBinding()

8.1.1.5.4 GetThisBinding()

  1. 返回未定义.

这篇关于为什么是“这个"?在 &lt;script type="module"&gt; 中未定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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