what is “strict mode” and how is it used?
我一直在查看Mozilla开发人员网络上的javascript参考,我遇到了一个叫做
它的主要目的是做更多的检查。
只需在代码顶部添加
例如,
但在严格模式下,这是一个错误,因为您没有使用关键字"var"来声明变量。
大多数情况下,您并不打算在某个任意范围的中间创建全局变量,因此大多数情况下,编写
同样,它也不允许做许多技术上有效的事情。
在严格模式下的MDN页面上阅读更多信息
Simon的答案中没有提到严格模式的一个方面是,在通过函数调用调用的函数中,严格模式将
所以像这样的事情
1 2 3 4 5 6 7 8 9 10 11 12 | function Obj() { this.a = 12; this.b ="a"; this.privilegedMethod = function () { this.a++; privateMethod(); }; function privateMethod() { this.b ="foo"; } } |
当调用
增加了严格的模式,这样就有了一个容易静态分析的ecmascript子集,这将是该语言未来版本的一个很好的目标。严格模式的设计也希望那些将自己限制在严格模式下的开发人员能够少犯错误,并且他们所犯的错误会以更明显的方式表现出来。
Harmony有望成为EcmaScript的下一个主要版本,它将建立在ES5 Strict之上。
Harmony builds on ES5 strict mode to avoid too many modes.
其他一些语言实验也依赖于严格的模式。SES依赖于ES5严格模式的可分析性。
SES (Secure ECMAScript) Design Experiment
Design an Object Capability Programming Language by removing or repairing features in ES5/Strict.
There should be a straight-forward translation from SES to ES5/Strict.
本标准的附录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 [?]).
- 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.
- 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.
- 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).
- 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).- 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).- 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).- 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).
- 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).
- 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).
- 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).- Strict mode code may not include a WithStatement. The occurrence of a WithStatement in such a context is an 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)
- It is a SyntaxError if the identifier eval or arguments appears within a FormalParameterList of a strict mode FunctionDeclaration or FunctionExpression (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).
- 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).
- 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.
ECMAScript 5 introduced the concept of strict mode.
在代码中调用严格模式
严格模式适用于整个脚本或单个函数。它不适用于括在大括号中的block语句,尝试将其应用于此类上下文没有任何作用。
整个脚本:
假设我们正在创建app.js,那么添加第一个语句使用脚本将对整个代码强制使用严格模式。
1 2 3 | // app.js whole script in strict mode syntax "use strict"; // Now you can start writing your code |
严格的功能模式:
要为函数调用严格模式,请将精确的语句"use strict";放在函数体开头的任何其他语句之前。
1 2 3 4 5 | function yourFunc(){ "use strict"; // Your function code logic } |
严格模式包含对普通JavaScript语义的几个更改。第一个严格模式通过将一些javascript静默错误更改为抛出错误来消除它们。
例如:使用严格模式的代码
在上面的代码示例中,如果不在代码中使用严格模式,则不会引发错误。当我们访问变量
现在,让我们尝试访问变量x,而不声明它而不使用严格模式。
1 2 3 4 5 | (function(){ x = 3; })(); // Will not throw an error |
使用严格模式的优点:
- 通过抛出错误消除javascript静默错误。
- 修复了使JavaScript引擎难以执行优化的错误。
- 使代码在某个时间比不在严格模式下的相同代码运行得更快
- 禁止在未来版本的ecmascript中定义某些语法。
严格模式对普通的javascript语义做了一些更改。
严格模式通过将某些javascript静默错误更改为引发错误来消除这些错误。
严格模式修复了使JavaScript引擎难以执行优化的错误。
严格模式禁止在未来版本的ecmascript中定义某些语法。
2017年,我终于找到了文件:
https://developer.mozilla.org/en-us/docs/web/javascript/reference/strict_模式
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.
< BR>
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.
严格模式是排除不推荐使用的功能的语言的子集。严格的模式是可选的,不需要,这意味着如果您希望代码在严格模式下,您声明您的意图使用(每个函数一次,或整个程序)以下字符串:
1 | "use strict"; |
问题:
下面是我遇到的问题,我正在学习一个教程,结果它试图编译下面的
1 2 3 | .fatty{ width: percentage(6/7); } |
使用以下
1 2 3 4 5 6 7 8 | 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')) }); |
所以我得到的错误如下:
1 2 3 4 5 6 7 8 | ~/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... |
解决方案:
所以它显示了我的gulp sass模块中的
我很无助,所以我一直用这个来解决问题!但是,在浏览了其他问题之后,我看到了以下答案:
1 2 | sudo npm install -g n sudo n stable |
我很快更新了nodejs(版本10.x),然后按照终端的指示运行以下命令重新构建了gulp:
1 | npm rebuild node-sass --force |
一切都好。这就是解决问题的方法。我撤消了对
希望这个答案对外面的人有帮助!