JavaScript:严格的模式和匿名函数

时间:2022-04-18 22:28:58

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?

几乎所有的JS文件都封装在匿名函数中。如果包括“使用严格”;在匿名函数之外,严格的模式仍然适用于匿名函数吗?

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?
})(); 

1 个解决方案

#1


8  

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.

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

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:

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

  • Global code is strict global code if it begins with a Directive Prologue that contains a Use Strict Directive (see 14.1).
  • 全局代码是严格的全局代码,如果它以包含使用严格指令的指令开头(参见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.
  • Eval代码是严格的Eval代码,如果它以包含Use strict Directive的指令序言开头,或者如果对Eval的调用是对包含在严格模式代码中的Eval函数的直接调用(参见15.1.2.1.1)。
  • 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.
  • 作为内置函数构造函数的最后一个参数提供的函数代码是严格的函数代码,如果最后一个参数是一个字符串,当作为函数体处理时,该字符串以包含Use strict指令的指令序言开始。

#1


8  

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.

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

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:

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

  • Global code is strict global code if it begins with a Directive Prologue that contains a Use Strict Directive (see 14.1).
  • 全局代码是严格的全局代码,如果它以包含使用严格指令的指令开头(参见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.
  • Eval代码是严格的Eval代码,如果它以包含Use strict Directive的指令序言开头,或者如果对Eval的调用是对包含在严格模式代码中的Eval函数的直接调用(参见15.1.2.1.1)。
  • 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.
  • 作为内置函数构造函数的最后一个参数提供的函数代码是严格的函数代码,如果最后一个参数是一个字符串,当作为函数体处理时,该字符串以包含Use strict指令的指令序言开始。