I've been looking over the JavaScript reference on the Mozilla Developer Network, and I came across something called "strict mode"
. I read it over and I'm having trouble understanding what it does. Can someone briefly explain (in general) what its purpose is and how it is useful?
我一直在查看Mozilla开发人员网络上的JavaScript参考资料,我遇到了一种叫做“严格模式”的东西。我把它读了一遍,弄不懂它是干什么的。有人能简单地(一般地)解释一下它的目的是什么以及它是如何有用的吗?
8 个解决方案
#1
138
Its main purpose is to do more checking.
它的主要目的是做更多的检查。
Just add "use strict";
at the top of your code, before anything else.
只需添加“使用严格的”;在代码的顶部,在任何事情之前。
For example, blah = 33;
is valid JavaScript. It means you create a completely global variable blah
.
例如,blah = 33;是有效的JavaScript。这意味着你要创建一个全局变量等等。
But in strict mode its an error because you did not use the keyword "var" to declare the variable.
但在严格模式下,这是一个错误,因为您没有使用关键字“var”来声明变量。
Most of the time you don't mean to create global variables in the middle of some arbitrary scope, so most of the time that blah = 33
is written it is an error and the programmer didn't actually want it to be a global variable, they meant to write var blah = 33
.
大多数时候你不想创建全局变量的任意范围,所以大部分的时间写废话= 33实际上是一个错误,程序员不希望它是一个全局变量,他们打算写var = 33。
It similarly disallows a lot of things that are technically valid to do. NaN = "lol"
does not produce an error. It also doesn't change the value of NaN. using strict this (and similar weird statements) produce errors. Most people appreciate this because there is no reason to ever write NaN = "lol"
, so there was most likely a typo.
它同样不允许很多技术上有效的事情。NaN =“lol”不会产生错误。它也不会改变NaN的值。使用严格的this(和类似的奇怪语句)会产生错误。大多数人都很欣赏这一点,因为没有理由去写NaN = "lol",所以很有可能是打错了。
Read more at the MDN page on strict mode
请阅读更多关于“严格模式”的MDN页面
#2
31
One aspect of strict mode not already mentioned in Simon's answer is that strict mode sets this
to undefined
in functions invoked through function invocation.
在Simon的回答中没有提到的严格模式的一个方面是,严格模式将它设置为通过函数调用调用调用的函数中未定义的模式。
So things like this
所以这样的事情
function Obj() {
this.a = 12;
this.b = "a";
this.privilegedMethod = function () {
this.a++;
privateMethod();
};
function privateMethod() {
this.b = "foo";
}
}
will cause an error when privateMethod
is called (since you can't add a property to undefined
), rather than uselessly adding a b
property to the global object.
将在调用privateMethod时导致错误(因为您不能向未定义的对象添加属性),而不是将一个b属性毫无用处地添加到全局对象。
#3
20
Strict mode was added so that there would be an easily statically-analyzable subset of EcmaScript which would be a good target for future versions of the language. Strict mode was also designed in the hope that developers who limit themselves to strict mode would make fewer mistakes and that the bugs they do make would manifest in more obvious ways.
添加了严格的模式,以便有一个易于静态分析的EcmaScript子集,这将是该语言未来版本的一个很好的目标。严格的模式也被设计出来,希望那些限制自己严格模式的开发人员会犯更少的错误,他们所做的错误会以更明显的方式表现出来。
Harmony, which will hopefully become the next major version of EcmaScript is going to be built on top of ES5 strict.
和谐,有望成为下一个主要版本的EcmaScript将在ES5 strict的基础上。
Harmony builds on ES5 strict mode to avoid too many modes.
和谐建立在ES5的严格模式上,以避免太多的模式。
Some other language experiments also depend on strict mode. SES depends on ES5 strict mode's analyzability.
其他一些语言实验也依赖于严格的模式。SES依赖于ES5严格模式的可分析性。
SES (Secure ECMAScript) Design Experiment
安全ECMAScript设计实验
Design an Object Capability Programming Language by removing or repairing features in ES5/Strict.
通过删除或修复ES5/Strict中的特性,设计一种对象能力编程语言。
There should be a straight-forward translation from SES to ES5/Strict.
应该有从SES到ES5/Strict的直接翻译。
Annex C of the standard explains the differences between strict mode and normal mode.
标准附件C解释了严格模式和正常模式的区别。
The strict mode restriction and exceptions
严格的模式限制和异常
- The identifiers "implements", "interface", "let", "package", "private", "protected", "public", "static", and "yield" are classified as FutureReservedWord tokens within strict mode code. (7.6.12 [?]).
- 标识符“实现”、“接口”、“let”、“package”、“private”、“protected”、“public”、“static”和“yield”在严格的模式代码中被归类为FutureReservedWord标记。(7.6.12[?])。
- A conforming implementation, when processing strict mode code, may not extend the syntax of NumericLiteral (7.8.3) to include OctalIntegerLiteral as described in B.1.1.
- 当处理严格的模式代码时,符合标准的实现可能不会扩展NumericLiteral(7.8.3)的语法来包括B.1.1中所描述的OctalIntegerLiteral。
- A conforming implementation, when processing strict mode code (see 10.1.1), may not extend the syntax of EscapeSequence to include OctalEscapeSequence as described in B.1.2.
- 当处理严格的模式代码(参见10.1.1)时,一个符合标准的实现可能不会扩展“逃逸”的语法,包括在B.1.2中描述的OctalEscapeSequence。
- Assignment to an undeclared identifier or otherwise unresolvable reference does not create a property in the global object. When a simple assignment occurs within strict mode code, its LeftHandSide must not evaluate to an unresolvable Reference. If it does a ReferenceError exception is thrown (8.7.2). The LeftHandSide also may not be a reference to a data property with the attribute value {[[Writable]]:false}, to an accessor property with the attribute value {[[Set]]:undefined}, nor to a non-existent property of an object whose [[Extensible]] internal property has the value false. In these cases a TypeError exception is thrown (11.13.1).
- 赋值给未声明的标识符或其他不可解引用不会在全局对象中创建属性。当一个简单的赋值在严格的模式代码中发生时,它的左边不能计算到一个不可解引用。如果它执行了一个ReferenceError异常(8.7.2)。左边也可能不是对具有属性值{[[可写]]:false}的数据属性的引用,也不是对具有属性值{[[[Set]]:undefined}的访问器属性的引用,也不是对具有[[[[]]]内部属性值为false的对象不存在的可扩展属性的引用。在这些情况下,抛出一个类型错误异常(11.13.1)。
- The identifier eval or arguments may not appear as the LeftHandSideExpression of an Assignment operator (11.13) or of a PostfixExpression (11.3) or as the UnaryExpression operated upon by a Prefix Increment (11.4.4) or a Prefix Decrement (11.4.5) operator. Arguments objects for strict mode functions define non-configurable accessor properties named "caller" and "callee" which throw a TypeError exception on access (10.6).
- 标识符或参数不能作为赋值操作符(11.13)或PostfixExpression(11.3)的左和边表达式出现,也不能作为前缀递增(11.4.4)或前缀递减(11.4.5)操作符操作的UnaryExpression出现。严格模式函数的参数对象定义了名为“调用者”和“callee”的非可配置访问器属性,它们在访问时抛出一个类型错误异常(10.6)。
- Arguments objects for strict mode functions do not dynamically share their array indexed property values with the corresponding formal parameter bindings of their functions. (10.6). For strict mode functions, if an arguments object is created the binding of the local identifier arguments to the arguments object is immutable and hence may not be the target of an assignment expression. (10.5).
- 严格模式函数的参数对象不会动态地与函数的相应形式参数绑定共享它们的数组索引属性值。(10.6)。对于严格的模式函数,如果参数对象被创建,则本地标识符参数与参数对象的绑定是不可变的,因此可能不是赋值表达式的目标。(10.5)。
- It is a SyntaxError if strict mode code contains an ObjectLiteral with more than one definition of any data property (11.1.5). It is a SyntaxError if the Identifier "eval" or the Identifier "arguments" occurs as the Identifier in a PropertySetParameterList of a PropertyAssignment that is contained in strict code or if its FunctionBody is strict code (11.1.5).
- 如果严格模式代码包含一个ObjectLiteral,并且任何数据属性的定义都不止一个(11.1.5),那么这就是SyntaxError。如果标识符“eval”或标识符“arguments”作为包含在严格代码中的PropertySetParameterList中的属性赋值的标识符出现,或者它的函数体是严格代码(11.1.5),那么它就是SyntaxError。
- Strict mode eval code cannot instantiate variables or functions in the variable environment of the caller to eval. Instead, a new variable environment is created and that environment is used for declaration binding instantiation for the eval code (10.4.2).
- 严格模式eval代码不能实例化调用者的变量环境中的变量或函数以进行eval。相反,会创建一个新的变量环境,该环境用于eval代码的声明绑定实例化(10.4.2)。
- If this is evaluated within strict mode code, then the this value is not coerced to an object. A this value of null or undefined is not converted to the global object and primitive values are not converted to wrapper objects. The this value passed via a function call (including calls made using Function.prototype.apply and Function.prototype.call) do not coerce the passed this value to an object (10.4.3, 11.1.1, 15.3.4.3, 15.3.4.4).
- 如果在严格模式代码中求值,则该值不会强制到对象。null或undefined的值不会转换为全局对象,原始值也不会转换为包装器对象。该值通过函数调用传递(包括使用Function.prototype进行的调用。apply和Function.prototype.call)不强制将传递给对象的值(10.4.3、11.1.1、15.3.4.3、15.3.4.4)。
- When a delete operator occurs within strict mode code, a SyntaxError is thrown if its UnaryExpression is a direct reference to a variable, function argument, or function name(11.4.1).
- 当删除操作符出现在严格模式代码中时,如果它的UnaryExpression是对变量、函数参数或函数名的直接引用,则抛出SyntaxError(11.4.1)。
- When a delete operator occurs within strict mode code, a TypeError is thrown if the property to be deleted has the attribute { [[Configurable]]:false } (11.4.1). It is a SyntaxError if a VariableDeclaration or VariableDeclarationNoIn occurs within strict code and its Identifier is eval or arguments (12.2.1).
- 当删除操作符出现在严格模式代码中时,如果要删除的属性具有属性{[[可配置]]:false}(11.4.1),则抛出一个TypeError。如果在严格的代码中发生了VariableDeclaration或VariableDeclaration noin,并且它的标识符是eval或参数(12.2.1),那么它就是一个SyntaxError。
- Strict mode code may not include a WithStatement. The occurrence of a WithStatement in such a context is an SyntaxError (12.10).
- 严格模式代码可能不包含WithStatement。在这样的上下文中出现WithStatement就是SyntaxError(12.10)。
- It is a SyntaxError if a TryStatement with a Catch occurs within strict code and the Identifier of the Catch production is eval or arguments (12.14.1)
- 如果一个带有Catch的TryStatement出现在严格的代码中,并且Catch产品的标识符是eval或parameters(12.14.1),那么它就是SyntaxError。
- It is a SyntaxError if the identifier eval or arguments appears within a FormalParameterList of a strict mode FunctionDeclaration or FunctionExpression (13.1)
- 如果标识符eval或参数出现在严格模式FunctionDeclaration或FunctionExpression的FormalParameterList中,那么它就是SyntaxError(13.1)。
- A strict mode function may not have two or more formal parameters that have the same name. An attempt to create such a function using a FunctionDeclaration, FunctionExpression, or Function constructor is a SyntaxError (13.1, 15.3.2).
- 一个严格的模式函数可能没有两个或多个具有相同名称的形式参数。使用FunctionDeclaration、FunctionExpression或function构造函数创建这样一个函数的尝试是SyntaxError(13.1, 15.3.2)。
- An implementation may not extend, beyond that defined in this specification, meanings within strict mode functions of properties named caller or arguments of function instances. ECMAScript code may not create or modify properties with these names on function objects that correspond to strict mode functions (10.6, 13.2, 15.3.4.5.3).
- 实现不能扩展,超出规范中定义的范围,在称为调用者的属性或函数实例的参数的严格模式函数中。ECMAScript代码不能在与严格模式函数对应的函数对象上使用这些名称创建或修改属性(10.6、13.2、15.3.4.5.3)。
- It is a SyntaxError to use within strict mode code the identifiers eval or arguments as the Identifier of a FunctionDeclaration or FunctionExpression or as a formal parameter name (13.1). Attempting to dynamically define such a strict mode function using the Function constructor (15.3.2) will throw a SyntaxError exception.
- 在严格模式代码中使用标识符eval或参数作为函数声明或函数表达式的标识符或形式参数名称(13.1)是SyntaxError。尝试使用函数构造函数(15.3.2)动态定义这样一个严格的模式函数将抛出SyntaxError异常。
#4
6
ECMAScript 5 introduced the concept of strict mode.
ECMAScript 5引入了严格模式的概念。
Invoking Strict Mode in Code
在代码中调用严格模式
Strict mode applies to entire scripts or to individual function. It doesn’t apply to block statement enclosed in {} braces, attempting to apply it to such contexts does nothing.
严格模式适用于整个脚本或单个函数。它不适用于包含在{}括号中的block语句,尝试将其应用于此类上下文没有任何作用。
Entire Script:
完整的脚本:
Let say we are creating app.js so adding first statement use script will enforce strict mode for entire code.
假设我们正在创建app.js,因此添加第一个语句使用脚本将对整个代码执行严格的模式。
// app.js whole script in strict mode syntax
“use strict”;
// Now you can start writing your code
Strict mode for function:
严格模式功能:
To Invoke strict mode for a function, put the exact statement “use strict”; in the start of function body before any other statement.
若要调用函数的严格模式,请输入确切的语句“使用严格”;在函数体的开始,在任何其他语句之前。
function yourFunc(){
"use strict";
// Your function code logic
}
Strict mode incorporate several changes to normal Javascript semantics. First strict mode eliminate some JavaScript silent error by changing them to throw errors.
严格的模式包含了对正常Javascript语义的一些更改。首先,严格模式通过更改为抛出错误来消除一些JavaScript静默错误。
For Instance: Code using Strict Mode
例如:使用严格模式的代码
In above code example without using strict mode in code It won't throw an error. As we are accessing variable x
without declaring it. So in strict mode accessing undeclared variable throw an error.
在上面的代码示例中,如果在代码中不使用严格模式,就不会抛出错误。因为我们访问变量x时没有声明它。所以在严格的模式下访问未声明的变量会抛出一个错误。
Now let's try to access variable x without declaring it without strict mode.
现在让我们尝试访问变量x而不声明它,没有严格的模式。
(function(){
x = 3;
})();
// Will not throw an error
Advantage of using strict mode:
使用严格模式的优点:
- Eliminate JavaScript silent errors by throwing error.
- 通过抛出错误消除JavaScript静默错误。
- Fixes mistake that make it difficult for JavaScript engine to perform optimisation.
- 修正了使JavaScript引擎难以执行优化的错误。
- Make code run faster sometime than identical code that’s not in strict mode
- 使代码在某些时候比不处于严格模式的相同代码运行得更快
- Prohibits some syntax likely to be defined in future version of ECMAScript.
- 禁止在未来版本的ECMAScript中可能定义某些语法。
#5
5
Strict mode makes several changes to normal JavaScript semantics.
严格模式对正常的JavaScript语义做了一些修改。
-
strict mode eliminates some JavaScript silent errors by changing them to throw errors.
严格模式通过将一些JavaScript静默错误更改为抛出错误来消除这些错误。
-
strict mode fixes mistakes that make it difficult for JavaScript engines to perform optimizations.
严格的模式修复错误,使JavaScript引擎难以执行优化。
-
strict mode prohibits some syntax likely to be defined in future versions of ECMAScript.
严格模式禁止在未来版本的ECMAScript中可能定义某些语法。
#6
1
ECMAScript5
introduces some new objects and properties and also the so-called "strict mode"
.
ECMAScript5引入了一些新的对象和属性,以及所谓的“严格模式”。
Strict mode is a subset of the language that excludes deprecated features. The strict mode is opt-in and not required, meaning that if you want your code to run in the strict mode, you declare your intention using (once per function, or once for the whole program) the following string:
严格模式是一种语言的子集,不包括弃用的特性。严格模式是可选择的,而不是必需的,这意味着如果您希望您的代码在严格模式下运行,您可以使用以下字符串(每个函数一次或整个程序一次)声明您的意图:
"use strict";
#7
0
2017 and I finally found the documentation:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode
2017年,我终于找到了文档:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode。
Strict mode is a way to opt in to a restricted variant of JavaScript. Strict mode isn't just a subset: it intentionally has different semantics from normal code. Browsers not supporting strict mode will run strict mode code with different behavior from browsers that do, so don't rely on strict mode without feature-testing for support for the relevant aspects of strict mode. Strict mode code and non-strict mode code can coexist, so scripts can opt into strict mode incrementally.
严格模式是一种选择受限制的JavaScript变体的方式。严格模式不仅仅是一个子集:它有意地具有与普通代码不同的语义。不支持严格模式的浏览器会运行严格的模式代码,与支持严格模式的浏览器有不同的行为,所以不要依赖没有特性测试的严格模式来支持严格模式的相关方面。严格模式代码和非严格模式代码可以共存,因此脚本可以逐步选择进入严格模式。
Strict mode makes several changes to normal JavaScript semantics. First, strict mode eliminates some JavaScript silent errors by changing them to throw errors. Second, strict mode fixes mistakes that make it difficult for JavaScript engines to perform optimizations: strict mode code can sometimes be made to run faster than identical code that's not strict mode. Third, strict mode prohibits some syntax likely to be defined in future versions of ECMAScript.
严格模式对正常的JavaScript语义做了一些修改。首先,严格模式通过更改为抛出错误来消除一些JavaScript静默错误。其次,严格模式修正了JavaScript引擎难以执行优化的错误:严格模式代码有时可以比不严格模式的相同代码运行得更快。第三,严格模式禁止在未来版本的ECMAScript中可能定义某些语法。
#8
0
Question:
Following is the problem I encountered, I was following a tutorial and it ended up trying to compile following scss
file and trying to generate CSS code from it,
问题:下面是我遇到的问题,我正在学习一个教程,它最终试图编译scss文件并试图从中生成CSS代码,
.fatty{
width: percentage(6/7);
}
using following gulpfile.js
task:
gulpfile后使用。js的任务:
var gulp = require('gulp');
var sass = require('gulp-sass');
gulp.task('sass', function () {
return gulp.src('app/scss/styles.scss')
.pipe(sass())
.pipe(gulp.dest('app/css'))
});
So the error I'm getting is as follows:
我得到的误差如下
~/htdocs/Learning/gulp1/node_modules/gulp-sass/index.js:66
let sassMap;
^^^
SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
// stacktrace here...
Solution:
So it shows me index.js
file which is inside my gulp-sass module (which is basically locked and shouldn't be edited). But if I go forcefully and add the "use_strict"
on the top of that index.js
file, it runs my task smoothly.
解决方案:它显示了索引。js文件,在我的gulp-sass模块中(它基本上是锁定的,不应该被编辑)。但是如果我在索引顶部加上"use_strict"js文件,它能很好地运行我的任务。
I was helpless, so I keep using this as the solution! But then after going through some other SO Q&A's I saw following answer as follows:
我很无助,所以我一直用这个作为解决方案!但是在经过其他一些问答之后,我看到了如下的答案:
sudo npm install -g n
sudo n stable
and sooner I updated my NodeJs (to Version10.x), and then rebuilt Gulp by running following commands as Terminal instructed me:
我很快更新了NodeJs(到Version10.x),然后按照终端指令运行以下命令重新构建了Gulp:
npm rebuild node-sass --force
And it's all okay. So that's how it got resolved. I undone the changes I did for index.js
gulp module file. And now it runs smoothly.
这都是好的。这就是解决问题的方法。我撤消了对索引的修改。js大口模块文件。现在一切顺利。
Hope this answer will be helpful to someone out there!
希望这个答案对外面的人有帮助!
#1
138
Its main purpose is to do more checking.
它的主要目的是做更多的检查。
Just add "use strict";
at the top of your code, before anything else.
只需添加“使用严格的”;在代码的顶部,在任何事情之前。
For example, blah = 33;
is valid JavaScript. It means you create a completely global variable blah
.
例如,blah = 33;是有效的JavaScript。这意味着你要创建一个全局变量等等。
But in strict mode its an error because you did not use the keyword "var" to declare the variable.
但在严格模式下,这是一个错误,因为您没有使用关键字“var”来声明变量。
Most of the time you don't mean to create global variables in the middle of some arbitrary scope, so most of the time that blah = 33
is written it is an error and the programmer didn't actually want it to be a global variable, they meant to write var blah = 33
.
大多数时候你不想创建全局变量的任意范围,所以大部分的时间写废话= 33实际上是一个错误,程序员不希望它是一个全局变量,他们打算写var = 33。
It similarly disallows a lot of things that are technically valid to do. NaN = "lol"
does not produce an error. It also doesn't change the value of NaN. using strict this (and similar weird statements) produce errors. Most people appreciate this because there is no reason to ever write NaN = "lol"
, so there was most likely a typo.
它同样不允许很多技术上有效的事情。NaN =“lol”不会产生错误。它也不会改变NaN的值。使用严格的this(和类似的奇怪语句)会产生错误。大多数人都很欣赏这一点,因为没有理由去写NaN = "lol",所以很有可能是打错了。
Read more at the MDN page on strict mode
请阅读更多关于“严格模式”的MDN页面
#2
31
One aspect of strict mode not already mentioned in Simon's answer is that strict mode sets this
to undefined
in functions invoked through function invocation.
在Simon的回答中没有提到的严格模式的一个方面是,严格模式将它设置为通过函数调用调用调用的函数中未定义的模式。
So things like this
所以这样的事情
function Obj() {
this.a = 12;
this.b = "a";
this.privilegedMethod = function () {
this.a++;
privateMethod();
};
function privateMethod() {
this.b = "foo";
}
}
will cause an error when privateMethod
is called (since you can't add a property to undefined
), rather than uselessly adding a b
property to the global object.
将在调用privateMethod时导致错误(因为您不能向未定义的对象添加属性),而不是将一个b属性毫无用处地添加到全局对象。
#3
20
Strict mode was added so that there would be an easily statically-analyzable subset of EcmaScript which would be a good target for future versions of the language. Strict mode was also designed in the hope that developers who limit themselves to strict mode would make fewer mistakes and that the bugs they do make would manifest in more obvious ways.
添加了严格的模式,以便有一个易于静态分析的EcmaScript子集,这将是该语言未来版本的一个很好的目标。严格的模式也被设计出来,希望那些限制自己严格模式的开发人员会犯更少的错误,他们所做的错误会以更明显的方式表现出来。
Harmony, which will hopefully become the next major version of EcmaScript is going to be built on top of ES5 strict.
和谐,有望成为下一个主要版本的EcmaScript将在ES5 strict的基础上。
Harmony builds on ES5 strict mode to avoid too many modes.
和谐建立在ES5的严格模式上,以避免太多的模式。
Some other language experiments also depend on strict mode. SES depends on ES5 strict mode's analyzability.
其他一些语言实验也依赖于严格的模式。SES依赖于ES5严格模式的可分析性。
SES (Secure ECMAScript) Design Experiment
安全ECMAScript设计实验
Design an Object Capability Programming Language by removing or repairing features in ES5/Strict.
通过删除或修复ES5/Strict中的特性,设计一种对象能力编程语言。
There should be a straight-forward translation from SES to ES5/Strict.
应该有从SES到ES5/Strict的直接翻译。
Annex C of the standard explains the differences between strict mode and normal mode.
标准附件C解释了严格模式和正常模式的区别。
The strict mode restriction and exceptions
严格的模式限制和异常
- The identifiers "implements", "interface", "let", "package", "private", "protected", "public", "static", and "yield" are classified as FutureReservedWord tokens within strict mode code. (7.6.12 [?]).
- 标识符“实现”、“接口”、“let”、“package”、“private”、“protected”、“public”、“static”和“yield”在严格的模式代码中被归类为FutureReservedWord标记。(7.6.12[?])。
- A conforming implementation, when processing strict mode code, may not extend the syntax of NumericLiteral (7.8.3) to include OctalIntegerLiteral as described in B.1.1.
- 当处理严格的模式代码时,符合标准的实现可能不会扩展NumericLiteral(7.8.3)的语法来包括B.1.1中所描述的OctalIntegerLiteral。
- A conforming implementation, when processing strict mode code (see 10.1.1), may not extend the syntax of EscapeSequence to include OctalEscapeSequence as described in B.1.2.
- 当处理严格的模式代码(参见10.1.1)时,一个符合标准的实现可能不会扩展“逃逸”的语法,包括在B.1.2中描述的OctalEscapeSequence。
- Assignment to an undeclared identifier or otherwise unresolvable reference does not create a property in the global object. When a simple assignment occurs within strict mode code, its LeftHandSide must not evaluate to an unresolvable Reference. If it does a ReferenceError exception is thrown (8.7.2). The LeftHandSide also may not be a reference to a data property with the attribute value {[[Writable]]:false}, to an accessor property with the attribute value {[[Set]]:undefined}, nor to a non-existent property of an object whose [[Extensible]] internal property has the value false. In these cases a TypeError exception is thrown (11.13.1).
- 赋值给未声明的标识符或其他不可解引用不会在全局对象中创建属性。当一个简单的赋值在严格的模式代码中发生时,它的左边不能计算到一个不可解引用。如果它执行了一个ReferenceError异常(8.7.2)。左边也可能不是对具有属性值{[[可写]]:false}的数据属性的引用,也不是对具有属性值{[[[Set]]:undefined}的访问器属性的引用,也不是对具有[[[[]]]内部属性值为false的对象不存在的可扩展属性的引用。在这些情况下,抛出一个类型错误异常(11.13.1)。
- The identifier eval or arguments may not appear as the LeftHandSideExpression of an Assignment operator (11.13) or of a PostfixExpression (11.3) or as the UnaryExpression operated upon by a Prefix Increment (11.4.4) or a Prefix Decrement (11.4.5) operator. Arguments objects for strict mode functions define non-configurable accessor properties named "caller" and "callee" which throw a TypeError exception on access (10.6).
- 标识符或参数不能作为赋值操作符(11.13)或PostfixExpression(11.3)的左和边表达式出现,也不能作为前缀递增(11.4.4)或前缀递减(11.4.5)操作符操作的UnaryExpression出现。严格模式函数的参数对象定义了名为“调用者”和“callee”的非可配置访问器属性,它们在访问时抛出一个类型错误异常(10.6)。
- Arguments objects for strict mode functions do not dynamically share their array indexed property values with the corresponding formal parameter bindings of their functions. (10.6). For strict mode functions, if an arguments object is created the binding of the local identifier arguments to the arguments object is immutable and hence may not be the target of an assignment expression. (10.5).
- 严格模式函数的参数对象不会动态地与函数的相应形式参数绑定共享它们的数组索引属性值。(10.6)。对于严格的模式函数,如果参数对象被创建,则本地标识符参数与参数对象的绑定是不可变的,因此可能不是赋值表达式的目标。(10.5)。
- It is a SyntaxError if strict mode code contains an ObjectLiteral with more than one definition of any data property (11.1.5). It is a SyntaxError if the Identifier "eval" or the Identifier "arguments" occurs as the Identifier in a PropertySetParameterList of a PropertyAssignment that is contained in strict code or if its FunctionBody is strict code (11.1.5).
- 如果严格模式代码包含一个ObjectLiteral,并且任何数据属性的定义都不止一个(11.1.5),那么这就是SyntaxError。如果标识符“eval”或标识符“arguments”作为包含在严格代码中的PropertySetParameterList中的属性赋值的标识符出现,或者它的函数体是严格代码(11.1.5),那么它就是SyntaxError。
- Strict mode eval code cannot instantiate variables or functions in the variable environment of the caller to eval. Instead, a new variable environment is created and that environment is used for declaration binding instantiation for the eval code (10.4.2).
- 严格模式eval代码不能实例化调用者的变量环境中的变量或函数以进行eval。相反,会创建一个新的变量环境,该环境用于eval代码的声明绑定实例化(10.4.2)。
- If this is evaluated within strict mode code, then the this value is not coerced to an object. A this value of null or undefined is not converted to the global object and primitive values are not converted to wrapper objects. The this value passed via a function call (including calls made using Function.prototype.apply and Function.prototype.call) do not coerce the passed this value to an object (10.4.3, 11.1.1, 15.3.4.3, 15.3.4.4).
- 如果在严格模式代码中求值,则该值不会强制到对象。null或undefined的值不会转换为全局对象,原始值也不会转换为包装器对象。该值通过函数调用传递(包括使用Function.prototype进行的调用。apply和Function.prototype.call)不强制将传递给对象的值(10.4.3、11.1.1、15.3.4.3、15.3.4.4)。
- When a delete operator occurs within strict mode code, a SyntaxError is thrown if its UnaryExpression is a direct reference to a variable, function argument, or function name(11.4.1).
- 当删除操作符出现在严格模式代码中时,如果它的UnaryExpression是对变量、函数参数或函数名的直接引用,则抛出SyntaxError(11.4.1)。
- When a delete operator occurs within strict mode code, a TypeError is thrown if the property to be deleted has the attribute { [[Configurable]]:false } (11.4.1). It is a SyntaxError if a VariableDeclaration or VariableDeclarationNoIn occurs within strict code and its Identifier is eval or arguments (12.2.1).
- 当删除操作符出现在严格模式代码中时,如果要删除的属性具有属性{[[可配置]]:false}(11.4.1),则抛出一个TypeError。如果在严格的代码中发生了VariableDeclaration或VariableDeclaration noin,并且它的标识符是eval或参数(12.2.1),那么它就是一个SyntaxError。
- Strict mode code may not include a WithStatement. The occurrence of a WithStatement in such a context is an SyntaxError (12.10).
- 严格模式代码可能不包含WithStatement。在这样的上下文中出现WithStatement就是SyntaxError(12.10)。
- It is a SyntaxError if a TryStatement with a Catch occurs within strict code and the Identifier of the Catch production is eval or arguments (12.14.1)
- 如果一个带有Catch的TryStatement出现在严格的代码中,并且Catch产品的标识符是eval或parameters(12.14.1),那么它就是SyntaxError。
- It is a SyntaxError if the identifier eval or arguments appears within a FormalParameterList of a strict mode FunctionDeclaration or FunctionExpression (13.1)
- 如果标识符eval或参数出现在严格模式FunctionDeclaration或FunctionExpression的FormalParameterList中,那么它就是SyntaxError(13.1)。
- A strict mode function may not have two or more formal parameters that have the same name. An attempt to create such a function using a FunctionDeclaration, FunctionExpression, or Function constructor is a SyntaxError (13.1, 15.3.2).
- 一个严格的模式函数可能没有两个或多个具有相同名称的形式参数。使用FunctionDeclaration、FunctionExpression或function构造函数创建这样一个函数的尝试是SyntaxError(13.1, 15.3.2)。
- An implementation may not extend, beyond that defined in this specification, meanings within strict mode functions of properties named caller or arguments of function instances. ECMAScript code may not create or modify properties with these names on function objects that correspond to strict mode functions (10.6, 13.2, 15.3.4.5.3).
- 实现不能扩展,超出规范中定义的范围,在称为调用者的属性或函数实例的参数的严格模式函数中。ECMAScript代码不能在与严格模式函数对应的函数对象上使用这些名称创建或修改属性(10.6、13.2、15.3.4.5.3)。
- It is a SyntaxError to use within strict mode code the identifiers eval or arguments as the Identifier of a FunctionDeclaration or FunctionExpression or as a formal parameter name (13.1). Attempting to dynamically define such a strict mode function using the Function constructor (15.3.2) will throw a SyntaxError exception.
- 在严格模式代码中使用标识符eval或参数作为函数声明或函数表达式的标识符或形式参数名称(13.1)是SyntaxError。尝试使用函数构造函数(15.3.2)动态定义这样一个严格的模式函数将抛出SyntaxError异常。
#4
6
ECMAScript 5 introduced the concept of strict mode.
ECMAScript 5引入了严格模式的概念。
Invoking Strict Mode in Code
在代码中调用严格模式
Strict mode applies to entire scripts or to individual function. It doesn’t apply to block statement enclosed in {} braces, attempting to apply it to such contexts does nothing.
严格模式适用于整个脚本或单个函数。它不适用于包含在{}括号中的block语句,尝试将其应用于此类上下文没有任何作用。
Entire Script:
完整的脚本:
Let say we are creating app.js so adding first statement use script will enforce strict mode for entire code.
假设我们正在创建app.js,因此添加第一个语句使用脚本将对整个代码执行严格的模式。
// app.js whole script in strict mode syntax
“use strict”;
// Now you can start writing your code
Strict mode for function:
严格模式功能:
To Invoke strict mode for a function, put the exact statement “use strict”; in the start of function body before any other statement.
若要调用函数的严格模式,请输入确切的语句“使用严格”;在函数体的开始,在任何其他语句之前。
function yourFunc(){
"use strict";
// Your function code logic
}
Strict mode incorporate several changes to normal Javascript semantics. First strict mode eliminate some JavaScript silent error by changing them to throw errors.
严格的模式包含了对正常Javascript语义的一些更改。首先,严格模式通过更改为抛出错误来消除一些JavaScript静默错误。
For Instance: Code using Strict Mode
例如:使用严格模式的代码
In above code example without using strict mode in code It won't throw an error. As we are accessing variable x
without declaring it. So in strict mode accessing undeclared variable throw an error.
在上面的代码示例中,如果在代码中不使用严格模式,就不会抛出错误。因为我们访问变量x时没有声明它。所以在严格的模式下访问未声明的变量会抛出一个错误。
Now let's try to access variable x without declaring it without strict mode.
现在让我们尝试访问变量x而不声明它,没有严格的模式。
(function(){
x = 3;
})();
// Will not throw an error
Advantage of using strict mode:
使用严格模式的优点:
- Eliminate JavaScript silent errors by throwing error.
- 通过抛出错误消除JavaScript静默错误。
- Fixes mistake that make it difficult for JavaScript engine to perform optimisation.
- 修正了使JavaScript引擎难以执行优化的错误。
- Make code run faster sometime than identical code that’s not in strict mode
- 使代码在某些时候比不处于严格模式的相同代码运行得更快
- Prohibits some syntax likely to be defined in future version of ECMAScript.
- 禁止在未来版本的ECMAScript中可能定义某些语法。
#5
5
Strict mode makes several changes to normal JavaScript semantics.
严格模式对正常的JavaScript语义做了一些修改。
-
strict mode eliminates some JavaScript silent errors by changing them to throw errors.
严格模式通过将一些JavaScript静默错误更改为抛出错误来消除这些错误。
-
strict mode fixes mistakes that make it difficult for JavaScript engines to perform optimizations.
严格的模式修复错误,使JavaScript引擎难以执行优化。
-
strict mode prohibits some syntax likely to be defined in future versions of ECMAScript.
严格模式禁止在未来版本的ECMAScript中可能定义某些语法。
#6
1
ECMAScript5
introduces some new objects and properties and also the so-called "strict mode"
.
ECMAScript5引入了一些新的对象和属性,以及所谓的“严格模式”。
Strict mode is a subset of the language that excludes deprecated features. The strict mode is opt-in and not required, meaning that if you want your code to run in the strict mode, you declare your intention using (once per function, or once for the whole program) the following string:
严格模式是一种语言的子集,不包括弃用的特性。严格模式是可选择的,而不是必需的,这意味着如果您希望您的代码在严格模式下运行,您可以使用以下字符串(每个函数一次或整个程序一次)声明您的意图:
"use strict";
#7
0
2017 and I finally found the documentation:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode
2017年,我终于找到了文档:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode。
Strict mode is a way to opt in to a restricted variant of JavaScript. Strict mode isn't just a subset: it intentionally has different semantics from normal code. Browsers not supporting strict mode will run strict mode code with different behavior from browsers that do, so don't rely on strict mode without feature-testing for support for the relevant aspects of strict mode. Strict mode code and non-strict mode code can coexist, so scripts can opt into strict mode incrementally.
严格模式是一种选择受限制的JavaScript变体的方式。严格模式不仅仅是一个子集:它有意地具有与普通代码不同的语义。不支持严格模式的浏览器会运行严格的模式代码,与支持严格模式的浏览器有不同的行为,所以不要依赖没有特性测试的严格模式来支持严格模式的相关方面。严格模式代码和非严格模式代码可以共存,因此脚本可以逐步选择进入严格模式。
Strict mode makes several changes to normal JavaScript semantics. First, strict mode eliminates some JavaScript silent errors by changing them to throw errors. Second, strict mode fixes mistakes that make it difficult for JavaScript engines to perform optimizations: strict mode code can sometimes be made to run faster than identical code that's not strict mode. Third, strict mode prohibits some syntax likely to be defined in future versions of ECMAScript.
严格模式对正常的JavaScript语义做了一些修改。首先,严格模式通过更改为抛出错误来消除一些JavaScript静默错误。其次,严格模式修正了JavaScript引擎难以执行优化的错误:严格模式代码有时可以比不严格模式的相同代码运行得更快。第三,严格模式禁止在未来版本的ECMAScript中可能定义某些语法。
#8
0
Question:
Following is the problem I encountered, I was following a tutorial and it ended up trying to compile following scss
file and trying to generate CSS code from it,
问题:下面是我遇到的问题,我正在学习一个教程,它最终试图编译scss文件并试图从中生成CSS代码,
.fatty{
width: percentage(6/7);
}
using following gulpfile.js
task:
gulpfile后使用。js的任务:
var gulp = require('gulp');
var sass = require('gulp-sass');
gulp.task('sass', function () {
return gulp.src('app/scss/styles.scss')
.pipe(sass())
.pipe(gulp.dest('app/css'))
});
So the error I'm getting is as follows:
我得到的误差如下
~/htdocs/Learning/gulp1/node_modules/gulp-sass/index.js:66
let sassMap;
^^^
SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
// stacktrace here...
Solution:
So it shows me index.js
file which is inside my gulp-sass module (which is basically locked and shouldn't be edited). But if I go forcefully and add the "use_strict"
on the top of that index.js
file, it runs my task smoothly.
解决方案:它显示了索引。js文件,在我的gulp-sass模块中(它基本上是锁定的,不应该被编辑)。但是如果我在索引顶部加上"use_strict"js文件,它能很好地运行我的任务。
I was helpless, so I keep using this as the solution! But then after going through some other SO Q&A's I saw following answer as follows:
我很无助,所以我一直用这个作为解决方案!但是在经过其他一些问答之后,我看到了如下的答案:
sudo npm install -g n
sudo n stable
and sooner I updated my NodeJs (to Version10.x), and then rebuilt Gulp by running following commands as Terminal instructed me:
我很快更新了NodeJs(到Version10.x),然后按照终端指令运行以下命令重新构建了Gulp:
npm rebuild node-sass --force
And it's all okay. So that's how it got resolved. I undone the changes I did for index.js
gulp module file. And now it runs smoothly.
这都是好的。这就是解决问题的方法。我撤消了对索引的修改。js大口模块文件。现在一切顺利。
Hope this answer will be helpful to someone out there!
希望这个答案对外面的人有帮助!