wrap-iife
要求在立即 function 调用周围加上括号
此规则报告的一些问题可通过 --fix 命令行 选项自动修复
This rule was deprecated in ESLint v8.53.0. It will be removed in v11.0.0. Please use the corresponding rule in @stylistic/eslint-plugin.
你可以立即调用函数表达式,但不能调用函数声明。创建立即调用函数表达式 (IIFE) 的常用技术是将函数声明封装在括号中。左括号导致包含的函数被解析为表达式,而不是声明。
¥You can immediately invoke function expressions, but not function declarations. A common technique to create an immediately-invoked function expression (IIFE) is to wrap a function declaration in parentheses. The opening parentheses causes the contained function to be parsed as an expression, rather than a declaration.
// function expression could be unwrapped
var x = function () { return { y: 1 };}();
// function declaration must be wrapped
function () { /* side effects */ }(); // SyntaxError
规则详情
¥Rule Details
此规则要求所有立即调用的函数表达式都用括号括起来。
¥This rule requires all immediately-invoked function expressions to be wrapped in parentheses.
选项
¥Options
该规则有两个选项,一个字符串选项和一个对象选项。
¥This rule has two options, a string option and an object option.
字符串选项:
¥String option:
-
"outside"强制始终封装调用表达式。默认值为"outside"。¥
"outside"enforces always wrapping the call expression. The default is"outside". -
"inside"强制始终封装函数表达式。¥
"inside"enforces always wrapping the function expression. -
"any"强制始终换行,但允许任一样式。¥
"any"enforces always wrapping, but allows either style.
对象选项:
¥Object option:
-
"functionPrototypeMethods": true还强制封装使用.call和.apply调用的函数表达式。默认值为false。¥
"functionPrototypeMethods": trueadditionally enforces wrapping function expressions invoked using.calland.apply. The default isfalse.
outside
默认 "outside" 选项的错误代码示例:
¥Examples of incorrect code for the default "outside" option:
/*eslint wrap-iife: ["error", "outside"]*/
var x = function () { return { y: 1 };}(); // unwrapped
var x = (function () { return { y: 1 };})(); // wrapped function expression
默认 "outside" 选项的正确代码示例:
¥Examples of correct code for the default "outside" option:
/*eslint wrap-iife: ["error", "outside"]*/
var x = (function () { return { y: 1 };}()); // wrapped call expression
inside
"inside" 选项的错误代码示例:
¥Examples of incorrect code for the "inside" option:
/*eslint wrap-iife: ["error", "inside"]*/
var x = function () { return { y: 1 };}(); // unwrapped
var x = (function () { return { y: 1 };}()); // wrapped call expression
"inside" 选项的正确代码示例:
¥Examples of correct code for the "inside" option:
/*eslint wrap-iife: ["error", "inside"]*/
var x = (function () { return { y: 1 };})(); // wrapped function expression
any
"any" 选项的错误代码示例:
¥Examples of incorrect code for the "any" option:
/*eslint wrap-iife: ["error", "any"]*/
var x = function () { return { y: 1 };}(); // unwrapped
"any" 选项的正确代码示例:
¥Examples of correct code for the "any" option:
/*eslint wrap-iife: ["error", "any"]*/
var x = (function () { return { y: 1 };}()); // wrapped call expression
var x = (function () { return { y: 1 };})(); // wrapped function expression
functionPrototypeMethods
使用 "inside", { "functionPrototypeMethods": true } 选项的此规则的错误代码示例:
¥Examples of incorrect code for this rule with the "inside", { "functionPrototypeMethods": true } options:
/* eslint wrap-iife: [2, "inside", { functionPrototypeMethods: true }] */
var x = function(){ foo(); }()
var x = (function(){ foo(); }())
var x = function(){ foo(); }.call(bar)
var x = (function(){ foo(); }.call(bar))
使用 "inside", { "functionPrototypeMethods": true } 选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the "inside", { "functionPrototypeMethods": true } options:
/* eslint wrap-iife: [2, "inside", { functionPrototypeMethods: true }] */
var x = (function(){ foo(); })()
var x = (function(){ foo(); }).call(bar)
版本
此规则是在 ESLint v0.0.9 中引入。