gpt4 book ai didi

javascript - 什么时候不应该在 JavaScript 中使用 "strict mode"?

转载 作者:行者123 更新时间:2023-12-02 22:27:04 24 4
gpt4 key购买 nike

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

我在这里理解的是,我应该使用严格始终

但我想知道,如果这是真的,那么使用“严格模式”总是更好,那么它甚至不会存在,因为这将是默认行为。

所以我搜索了 ECMAScript 第六版定义,发现它是很多情况下的默认定义。

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.

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

提前致谢。

最佳答案

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

当您运行尚 future 得及更新的旧(或第三方)代码时。

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

关于javascript - 什么时候不应该在 JavaScript 中使用 "strict mode"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44587711/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com