什么时候不在 javascript 中使用 "strict mode"?

When to NOT use "strict mode" in javascript?

我找到了这个 post-What does "use strict" do in JavaScript, and what is the reasoning behind it?

我在这里的理解是我应该use strict总是

但我想知道,如果这是真的,那么使用 "strict mode" 总是更好,那么它甚至不会存在,因为这是默认行为。

所以我搜索了 ECMAScript 6th edition 定义,发现它是很多情况下的默认值。

根据official documentation about strict mode

An ECMAScript Script syntactic unit may be processed using either unrestricted or strict mode syntax and semantics. The 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.

ECMAScript code that is not strict mode code is called non-strict code.

那么,什么时候使用非严格代码是个不错的选择?

提前致谢。

根据下面的 MDN link:-

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Strict_mode

Strict mode in browsers

The major browsers now implement strict mode. However, don't blindly depend on it since there still are numerous Browser versions used in the wild that only have partial support for strict mode or do not support it at all (e.g. Internet Explorer below version 10!). Strict mode changes semantics. Relying on those changes will cause mistakes and errors in browsers which don't implement strict mode. Exercise caution in using strict mode, and back up reliance on strict mode with feature tests that check whether relevant parts of strict mode are implemented. Finally, make sure to test your code in browsers that do and don't support strict mode. If you test only in browsers that don't support strict mode, you're very likely to have problems in browsers that do, and vice versa.

So, when is a good choice to not use strict mode?

严格模式在发现未声明的变量时以及在某些情况下会抛出引用错误。

如果你有这样一个不受限制的类型代码,那就是使用变量而不声明。一个变量在某些 function/scope 中声明并从其他地方使用(它将在那里未声明)并且你不能 rewrite/change 它们,那么你不应该使用 "use strict;" 模式,因为它会中断代码。

来自MDN

Strict mode makes several changes to normal JavaScript semantics.

  1. strict mode eliminates some JavaScript silent errors by changing them to throw errors.
  2. 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.
  3. strict mode prohibits some syntax likely to be defined in future versions of ECMAScript.

strict mode

您不能像在非严格模式下那样只使用变量而不声明它们

也就是

以下将起作用,因为它是非严格模式

a = "Hello World";
console.log(a);

以下将无法工作,因为它处于严格模式

'use strict';
a = "Hello World";  // => Throw a reference error
console.log(a);

上面的代码会抛出引用错误,因为变量 a 没有声明就被使用了。

所以,你应该使用

'use strict';
var a = "Hello World";
console.log(a);

非严格模式下的例子

a = "Hello World";
console.log(a);

错误的严格模式示例

'use strict';
a = "Hello World";
console.log(a);

严格模式下没有错误的例子

'use strict';
var a = "Hello World";
console.log(a);

简而言之

严格模式会给你带来很多限制,但可能会提高性能,如 MDN 所述。还减少了 JavaScript 引擎的负载。

提示

如果你想从非严格模式迁移到严格模式,你应该非常小心每一行代码和变量及其范围。

So, when is a good choice to not use strict mode?

当您 运行 没有时间更新的旧(或第三方)代码时。

严格模式更好。默认情况下它不启用,因为它会破坏未考虑到它编写的旧代码。

So, when is a good choice to not use strict mode?

如果您的 javascript 代码是由第三方库生成或编译的,则添加到其他答案中。你不想操纵它,因为你没有写它。