不推荐使用“使用严格”在ES6? [英] Not recommended to use "use strict" in ES6?

查看:124
本文介绍了不推荐使用“使用严格”在ES6?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不熟悉ECMAScript 6。我刚刚克隆了React Starter Kit repo,它使用ES6作为应用程序代码。我很惊讶地发现,短信是配置以禁止的发生使用严格的指令,我认为这是在ES6之前的JavaScript中推荐的。那么有什么意义?

解决方案

ES6模块始终处于严格模式。引用规范的相关部分:


10.2.1严格模式代码



ECMAScript脚本语法单元可以使用无限制或严格模式的语法和语义。代码在以下情况下被解释为严格模式代码:




  • 如果全局代码是以包含一个使用严格指令(见14.1.1)。

  • 模块代码始终是严格的模式代码。

  • 一个ClassDeclaration或一个ClassExpression是严格的模式代码。

  • 评估代码是严格的模式代码,如果它开始于包含使用严格指令的指令序言,或者如果对eval的调用是直接的eval (见12.3.4.1),包含在严格模式代码中。

  • 功能代码是严格模式代码,如果相关的FunctionDeclaration,FunctionExpression,GeneratorDeclaration,GeneratorExpression,MethodDefinition或ArrowFunction包含在strict模式代码或产生函数[[ECMAScriptCode]]内部插槽值的代码以包含使用严格指令的指令序言开始。

  • 作为内置函数和生成器构造函数的参数提供的函数代码是严格模式代码,如果最后一个参数是一个String,当处理的是一个以包含使用严格指令的指令序言开头的FunctionBody。


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 configured to forbid occurences of the use strict directive, which I thought was recommended in pre-ES6 JavaScript. So what's the point?

解决方案

ES6 modules are always in strict mode. To quote the relevant part of the spec:

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.

这篇关于不推荐使用“使用严格”在ES6?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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