JavaScript:严格模式和匿名函数

发布于 2024-11-07 17:36:05 字数 230 浏览 0 评论 0原文

我的几乎所有 JS 文件都包含在匿名函数中。如果我在匿名函数之外包含 "use strict"; ,严格模式是否仍适用于匿名函数?

例如,严格模式是否应用于以下脚本中匿名函数的内部主体:

"use strict";

(function() {
    // Is this code running under strict mode?
})(); 

Nearly all my JS files are wrapped in anonymous functions. If I include "use strict"; outside the anonymous function, is strict mode still applied to the anonymous function?

For example, is strict mode applied to the inner body of the anonymous function in the script below:

"use strict";

(function() {
    // Is this code running under strict mode?
})(); 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七七 2024-11-14 17:36:05

根据 John Resig 的文章,如果您打开严格模式位于文件顶部,它适用于整个文件/脚本。所以是的,这意味着它将在匿名函数中应用。

您还可以将其添加到函数中,在这种情况下,它仅适用于该特定函数。

编辑添加:这是完整规范。相关段落:

10.1.1 严格模式代码

ECMAScript 程序语法单元可以使用非限制或严格模式语法和语义进行处理。当使用严格模式处理时,三种类型的 ECMAScript 代码被称为严格全局代码、严格 eval 代码和严格函数代码。在以下情况下,代码被解释为严格模式代码:

  • 如果全局代码以包含使用严格指令的指令序言开头(请参阅 14.1),则全局代码是严格全局代码。
  • 如果 Eval 代码以包含“使用严格指令”的指令序言开头,或者对 eval 的调用是对严格模式中包含的 eval 函数的直接调用(请参阅 15.1.2.1.1),则它是严格的 eval 代码代码。
  • 作为 FunctionDeclaration、FunctionExpression 或访问器 PropertyAssignment 一部分的函数代码是严格函数代码,如果其 FunctionDeclaration、FunctionExpression 或 PropertyAssignment 包含在严格模式代码中,或者如果函数代码以包含 Use Strict 的指令序言开头指示。
  • 如果最后一个参数是一个字符串,并且在作为 FunctionBody 处理时以包含使用严格指令的指令序言开头,则作为内置 Function 构造函数的最后一个参数提供的函数代码是严格函数代码。

According to John Resig's article, if you turn on strict mode at the top of the file, it applies to the entire file/script. So yes, that implies that it would apply within the anonymous function.

You can also add it within a function, in which case it only applies to that specific function.

Edited to add: here's the full specification. One relevant paragraph:

10.1.1 Strict Mode Code

An ECMAScript Program syntactic unit may be processed using either unrestricted or strict mode syntax and semantics. When processed using strict mode the three types of ECMAScript code are referred to as strict global code, strict eval code, and strict function code. Code is interpreted as strict mode code in the following situations:

  • Global code is strict global code if it begins with a Directive Prologue that contains a Use Strict Directive (see 14.1).
  • Eval code is strict eval code if it begins with a Directive Prologue that contains a Use Strict Directive or if the call to eval is a direct call (see 15.1.2.1.1) to the eval function that is contained in strict mode code.
  • Function code that is part of a FunctionDeclaration, FunctionExpression, or accessor PropertyAssignment is strict function code if its FunctionDeclaration, FunctionExpression, or PropertyAssignment is contained in strict mode code or if the function code begins with a Directive Prologue that contains a Use Strict Directive.
  • Function code that is supplied as the last argument to the built-in Function constructor is strict function code if the last argument is a String that when processed as a FunctionBody begins with a Directive Prologue that contains a Use Strict Directive.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文