wrap-iife
立即调用 function
需要括号
一些该规则报告的问题可以通过 --fix 命令行选项 自动修复
您可以立即调用函数表达式,但不能调用函数声明。创建立即调用函数表达式 (IIFE) 的常用技术是将函数声明包装在括号中。左括号导致包含的函数被解析为表达式,而不是声明。
// function expression could be unwrapped
var x = function () { return { y: 1 };}();
// function declaration must be wrapped
function () { /* side effects */ }(); // SyntaxError
规则详情
此规则要求所有立即调用的函数表达式都用括号括起来。
选项
该规则有两个选项,一个字符串选项和一个对象选项。
QxQuvpwgm0mgOftYdWYBhSlCpkP85liGpf1aVQXKtm8=
- 0wnqnIEZ71NlWuNZmJmw19TISv31iWBpIoAIhwyZlOqBML9Y2ICuGD/RRCZMQCZ6AO9Q6V3S8rOIpUiS8/j1XNuz/CYtA5VXMfupOIG3i0RqBHklRyGcV0vOLBURG5R4vPM1Uto7YBWmnOe3H+6y7A2FJAD7DhoJRR7y23bmmSzXxOpVFaMMnJPSu85GAZJPsffbX0njptZlFO2f0ckNM0qREwcQd12sgqdk0RoPV3RmSVH8Y3rKogPl1aygTtIYRmN7CBJesNeYPL6b722oi6aEdKfQcakla/oT6uzeEqMlvc9Y94NItdn4RiM71Czt71UBD6MrCr8Hx1/vJ4pedu3h3ObSuxAQMJmQ24j31TU=
tW9GM1KVdz60b0xpoPIk+w==
- xE0ZL2I27OYGhz88Cg2mSUmbH1fFKmq4CijFmL49LvdSACnEWjmwZZ2bK930z9Hhl+2fpJPjfEnNS20faeBm79Jub4gC/GyT7K8IqfudrmebfusjWboX4s1IsFsPW5AMS8Vs7+ui4KcvxT9CgewGkQUlLwidrpP1W1gDhEtQoDnOTzhgb+Pn2FQvVwhiQv9TwF1U7CPcs44cpIzHXH/vkjcXbeuBVwhG0HzB4W/JwW1A4QYd13fPNIE2vn5a5YJ5
outside
ISJy0uRBur+5tW8v6/yycE1wswxyjfvjhQq04UF7j377Hx8iBncVyAHTubpU3ta/LOtDJXrGFwxbU6/ASW063A==
/*eslint wrap-iife: ["error", "outside"]*/
var x = function () { return { y: 1 };}(); // unwrapped
var x = (function () { return { y: 1 };})(); // wrapped function expression
ISJy0uRBur+5tW8v6/yycE1wswxyjfvjhQq04UF7j35ArSfo5p4aRJMPCVhtjpo3p+ZFmGorceLFpNlsAA2GBg==
/*eslint wrap-iife: ["error", "outside"]*/
var x = (function () { return { y: 1 };}()); // wrapped call expression
inside
n3B4w25fuVhR0R8ILjQIk+MAAAmHx4zxjlqGIs9zqr6+b44TRWNG87Jqg0R/YxHnC+w2R9n0b5LhKtjiYI+y8A==
/*eslint wrap-iife: ["error", "inside"]*/
var x = function () { return { y: 1 };}(); // unwrapped
var x = (function () { return { y: 1 };}()); // wrapped call expression
n3B4w25fuVhR0R8ILjQIk+0WKd3rV770OR7pFKCmaC0ETS0VVMKLEuhQ9+2hH03wJk9lxkQasYhu6/FVc9X7rQ==
/*eslint wrap-iife: ["error", "inside"]*/
var x = (function () { return { y: 1 };})(); // wrapped function expression
any
wqCiqvx15ElI29RLUEXUhPHv/zJ/Mb0SghHhojHBMZsVU++5YgFYgpa8nIEv9IjpgKOxxBHRZeAhx/rohvJJng==
/*eslint wrap-iife: ["error", "any"]*/
var x = function () { return { y: 1 };}(); // unwrapped
wqCiqvx15ElI29RLUEXUhO3Dz3RQ7AXmsxxtlGwN+3YfShNoufitjSop2C4u/4yrd/Jx8+xoJNUJfKAWWUXsgA==
/*eslint wrap-iife: ["error", "any"]*/
var x = (function () { return { y: 1 };}()); // wrapped call expression
var x = (function () { return { y: 1 };})(); // wrapped function expression
functionPrototypeMethods
Bv4sMSnguAQJ4kJJZCKoguwLRddWssF027dpro+8jRc02jqrrmKsMDUdR+40juanZW99qVM2JwGprzv865HMkIbRRPQ1Ih46XY4V3kLDWr2hKW5J3Bl0YPx+pOblgJxfhbCKaiTrlNvygbr9Ezu2qw==
/* 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))
Bv4sMSnguAQJ4kJJZCKoguwLRddWssF027dpro+8jRc02jqrrmKsMDUdR+40juanZW99qVM2JwGprzv865HMkIbRRPQ1Ih46XY4V3kLDWr2WKeckfZ5FFbMXe00BJY6vR5O4/pPit/hbEIIwhqVsPw==
/* eslint wrap-iife: [2, "inside", { functionPrototypeMethods: true }] */
var x = (function(){ foo(); })()
var x = (function(){ foo(); }).call(bar)