Javascript 不建议在 ES6 中使用“use strict”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31685262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Not recommended to use "use strict" in ES6?
提问by Midiparse
I'm not familiar with ECMAScript 6 yet. I've just cloned the React Starter Kit repo, which uses ES6 for application code. I was surprised to see that the linter is configuredto forbid occurences of the use strict
directive, which I thought was recommended in pre-ES6 JavaScript. So what's the point?
我还不熟悉 ECMAScript 6。我刚刚克隆了 React Starter Kit 存储库,它使用 ES6 作为应用程序代码。我很惊讶地看到 linter 被配置为禁止出现use strict
指令,我认为这是在 ES6 之前的 JavaScript 中推荐的。那么有什么意义呢?
回答by Kit Sunde
ES6 modules are always in strict mode. To quote the relevant part of the spec:
ES6 模块始终处于严格模式。引用规范的相关部分:
10.2.1 Strict Mode Code
An ECMAScript Script syntactic unit may be processed using either unrestricted or strict mode syntax and semantics. Code is interpreted as strict mode code in the following situations:
- Global code is strict mode code if it begins with a Directive Prologue that contains a Use Strict Directive (see 14.1.1).
- Module code is always strict mode code.
- All parts of a ClassDeclaration or a ClassExpression are strict mode code.
- Eval code is strict mode code if it begins with a Directive Prologue that contains a Use Strict Directive or if the call to eval is a direct eval (see 12.3.4.1) that is contained in strict mode code.
- Function code is strict mode code if the associated FunctionDeclaration, FunctionExpression, GeneratorDeclaration, GeneratorExpression, MethodDefinition, or ArrowFunction is contained in strict mode code or if the code that produces the value of the function's [[ECMAScriptCode]] internal slot begins with a Directive Prologue that contains a Use Strict Directive.
- Function code that is supplied as the arguments to the built-in Function and Generator constructors is strict mode code if the last argument is a String that when processed is a FunctionBody that begins with a Directive Prologue that contains a Use Strict Directive.
10.2.1 严格模式代码
ECMAScript Script 语法单元可以使用不受限制或严格模式的语法和语义进行处理。在以下情况下,代码被解释为严格模式代码:
- 如果全局代码以包含使用严格指令的指令序言开头(参见 14.1.1),则它是严格模式代码。
- 模块代码始终是严格模式代码。
- ClassDeclaration 或 ClassExpression 的所有部分都是严格模式代码。
- 如果 Eval 代码以包含使用严格指令的指令序言开头,或者如果对 eval 的调用是包含在严格模式代码中的直接 eval(参见 12.3.4.1),则它是严格模式代码。
- 如果相关的 FunctionDeclaration、FunctionExpression、GeneratorDeclaration、GeneratorExpression、MethodDefinition 或 ArrowFunction 包含在严格模式代码中,或者产生函数 [[ECMAScriptCode]] 内部槽值的代码以指令序言开头,则函数代码是严格模式代码包含使用严格指令。
- 如果最后一个参数是一个字符串,则作为参数提供给内置函数和生成器构造函数的函数代码是严格模式代码,当处理时它是一个以包含使用严格指令的指令序言开头的函数体。