javascript - 什么是扩展模式?

标签 javascript ecmascript-6

我正在玩弄 Node v0.10.12 中的 let。使用 --harmony 标志,以下代码会产生语法错误:

for (let i = 0; i < 2; i += 1) {
    console.log('i', i);
}

SyntaxError: Illegal let declaration outside extended mode

但是,如果我也使用 --use-strict 标志,那么代码会按预期运行。

为什么仅使用 --harmony 标志时会抛出语法错误?什么是扩展模式?和严格模式有什么关系?

最佳答案

看起来“扩展模式”已从 current development version 中删除2012 年 2 月 27 日的和声规范,但有一些旧版本的描述(这个来自 2012 年 1 月 16 日):

10.1.2 Extended Code

Extended code is any code contained in an ECMAScript Program syntactic unit that contains occurrences of lexical or syntactic productions defined subsequent to the Fifth Edition of the ECMAScript specification. Code is interpreted as extended code in the following situations:

  • Global code is extended global code if it is contained in an ECMAScript Program syntactic unit that has been designated as an extended Program unit in an implementation defined manner or if ???.

  • Eval code is extended eval code if the call to eval is a direct call (see 15.1.2.1.1) to the eval function that is contained in extended mode code or if it begins with ???.

  • Function code that is part of a FunctionDeclaration, FunctionExpression, or accessor PropertyAssignment is extended function code if its FunctionDeclaration, FunctionExpression, or PropertyAssignment is contained in extended mode code or if the function code begins with ???.

  • Function code that is supplied as the last argument to the built-in Function constructor is strict function code if the last argument is a String that when processed as a FunctionBody begins with ???.

The term “strict code” is used to designate both actual strict mode code and extended code while the term “extended code” only designates actual extended code. The term “base code” is used to designate code that is not extended code.

至于与严格模式的联系,这似乎是 V8 的(实验性)实现所特有的。这是 changelog for revision 10062 的内容,引入了 --harmony 标志,说:

This CL introduces a third mode next to the non-strict (henceforth called 'classic mode') and 'strict mode' which is called 'extended mode' as in the current ES.next specification drafts. The extended mode is based on the 'strict mode' and adds new functionality to it. This means that most of the semantics of these two modes coincide.

The 'extended mode' is entered instead of the 'strict mode' during parsing when using the 'strict mode' directive "use strict" and when the the harmony-scoping flag is active. This should be changed once it is fully specified how the 'extended mode' is entered.

关于javascript - 什么是扩展模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17253509/

相关文章:

javascript - 在 CoffeeScript 中将函数作为参数传递

javascript - 更改 html PreventDefault

javascript - 如何在 Redux 中向数组添加项目?

javascript - es6 传播运算符 - mongoose 结果副本

javascript - ui-router嵌套状态和中介守卫

javascript - 如何基于对象键/值配对创建有序列表?

php - AJAX无法识别json数组

javascript - D3 : setting style conditionally with immediately invoked arrow function and ternary

javascript - Object.assign 错误解决方法

在另一个成员函数中调用 Javascript 成员函数