【问题标题】:why is "this" undefined in <script type="module">?为什么在 <script type="module"> 中未定义“this”?
【发布时间】:2018-07-18 03:27:43
【问题描述】:

玩转 ECMAScript 模块后发现,在具有模块类型的脚本的“全局”范围内调用 thisundefined,但在普通脚本中,它引用 window 对象

ES 模块脚本

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

普通脚本

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

对此有什么规范吗,或者有人可以解释一下原因吗?

【问题讨论】:

标签: javascript html ecmascript-6


【解决方案1】:

ES6 模块的 this 值是 undefined。您可以在8.1.1.5.4 Module Environment Records: GetThisBinding() 中查看规范定义。

8.1.1.5.4 GetThisBinding()

  1. 返回未定义。

【讨论】:

猜你喜欢
  • 2018-08-17
  • 2021-01-14
  • 2018-11-17
  • 1970-01-01
相关资源
最近更新 更多