9 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.
12 // function expression could be unwrapped
13 var x = function () { return { y: 1 };}();
15 // function declaration must be wrapped
16 function () { /* side effects */ }(); // SyntaxError
21 This rule requires all immediately-invoked function expressions to be wrapped in parentheses.
25 This rule has two options, a string option and an object option.
29 * `"outside"` enforces always wrapping the *call* expression. The default is `"outside"`.
30 * `"inside"` enforces always wrapping the *function* expression.
31 * `"any"` enforces always wrapping, but allows either style.
35 * `"functionPrototypeMethods": true` additionally enforces wrapping function expressions invoked using `.call` and `.apply`. The default is `false`.
39 Examples of **incorrect** code for the default `"outside"` option:
44 /*eslint wrap-iife: ["error", "outside"]*/
46 var x = function () { return { y: 1 };}(); // unwrapped
47 var x = (function () { return { y: 1 };})(); // wrapped function expression
52 Examples of **correct** code for the default `"outside"` option:
57 /*eslint wrap-iife: ["error", "outside"]*/
59 var x = (function () { return { y: 1 };}()); // wrapped call expression
66 Examples of **incorrect** code for the `"inside"` option:
71 /*eslint wrap-iife: ["error", "inside"]*/
73 var x = function () { return { y: 1 };}(); // unwrapped
74 var x = (function () { return { y: 1 };}()); // wrapped call expression
79 Examples of **correct** code for the `"inside"` option:
84 /*eslint wrap-iife: ["error", "inside"]*/
86 var x = (function () { return { y: 1 };})(); // wrapped function expression
93 Examples of **incorrect** code for the `"any"` option:
98 /*eslint wrap-iife: ["error", "any"]*/
100 var x = function () { return { y: 1 };}(); // unwrapped
105 Examples of **correct** code for the `"any"` option:
110 /*eslint wrap-iife: ["error", "any"]*/
112 var x = (function () { return { y: 1 };}()); // wrapped call expression
113 var x = (function () { return { y: 1 };})(); // wrapped function expression
118 ### functionPrototypeMethods
120 Examples of **incorrect** code for this rule with the `"inside", { "functionPrototypeMethods": true }` options:
125 /* eslint wrap-iife: [2, "inside", { functionPrototypeMethods: true }] */
127 var x = function(){ foo(); }()
128 var x = (function(){ foo(); }())
129 var x = function(){ foo(); }.call(bar)
130 var x = (function(){ foo(); }.call(bar))
135 Examples of **correct** code for this rule with the `"inside", { "functionPrototypeMethods": true }` options:
140 /* eslint wrap-iife: [2, "inside", { functionPrototypeMethods: true }] */
142 var x = (function(){ foo(); })()
143 var x = (function(){ foo(); }).call(bar)