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"。"inside"强制总是对 function 表达式进行换行。"any"强制总是换行,但允许任意一种风格。
对象选项:
🌐 Object option:
"functionPrototypeMethods": true还会强制对使用.call和.apply调用的函数表达式进行封装。默认值是false。
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 中引入。