]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/wrap-iife.md
import 8.23.1 source
[pve-eslint.git] / eslint / docs / src / rules / wrap-iife.md
1 ---
2 title: wrap-iife
3 layout: doc
4 rule_type: layout
5 ---
6
7
8
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.
10
11 ```js
12 // function expression could be unwrapped
13 var x = function () { return { y: 1 };}();
14
15 // function declaration must be wrapped
16 function () { /* side effects */ }(); // SyntaxError
17 ```
18
19 ## Rule Details
20
21 This rule requires all immediately-invoked function expressions to be wrapped in parentheses.
22
23 ## Options
24
25 This rule has two options, a string option and an object option.
26
27 String option:
28
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.
32
33 Object option:
34
35 * `"functionPrototypeMethods": true` additionally enforces wrapping function expressions invoked using `.call` and `.apply`. The default is `false`.
36
37 ### outside
38
39 Examples of **incorrect** code for the default `"outside"` option:
40
41 ::: incorrect
42
43 ```js
44 /*eslint wrap-iife: ["error", "outside"]*/
45
46 var x = function () { return { y: 1 };}(); // unwrapped
47 var x = (function () { return { y: 1 };})(); // wrapped function expression
48 ```
49
50 :::
51
52 Examples of **correct** code for the default `"outside"` option:
53
54 ::: correct
55
56 ```js
57 /*eslint wrap-iife: ["error", "outside"]*/
58
59 var x = (function () { return { y: 1 };}()); // wrapped call expression
60 ```
61
62 :::
63
64 ### inside
65
66 Examples of **incorrect** code for the `"inside"` option:
67
68 ::: incorrect
69
70 ```js
71 /*eslint wrap-iife: ["error", "inside"]*/
72
73 var x = function () { return { y: 1 };}(); // unwrapped
74 var x = (function () { return { y: 1 };}()); // wrapped call expression
75 ```
76
77 :::
78
79 Examples of **correct** code for the `"inside"` option:
80
81 ::: correct
82
83 ```js
84 /*eslint wrap-iife: ["error", "inside"]*/
85
86 var x = (function () { return { y: 1 };})(); // wrapped function expression
87 ```
88
89 :::
90
91 ### any
92
93 Examples of **incorrect** code for the `"any"` option:
94
95 ::: incorrect
96
97 ```js
98 /*eslint wrap-iife: ["error", "any"]*/
99
100 var x = function () { return { y: 1 };}(); // unwrapped
101 ```
102
103 :::
104
105 Examples of **correct** code for the `"any"` option:
106
107 ::: correct
108
109 ```js
110 /*eslint wrap-iife: ["error", "any"]*/
111
112 var x = (function () { return { y: 1 };}()); // wrapped call expression
113 var x = (function () { return { y: 1 };})(); // wrapped function expression
114 ```
115
116 :::
117
118 ### functionPrototypeMethods
119
120 Examples of **incorrect** code for this rule with the `"inside", { "functionPrototypeMethods": true }` options:
121
122 ::: incorrect
123
124 ```js
125 /* eslint wrap-iife: [2, "inside", { functionPrototypeMethods: true }] */
126
127 var x = function(){ foo(); }()
128 var x = (function(){ foo(); }())
129 var x = function(){ foo(); }.call(bar)
130 var x = (function(){ foo(); }.call(bar))
131 ```
132
133 :::
134
135 Examples of **correct** code for this rule with the `"inside", { "functionPrototypeMethods": true }` options:
136
137 ::: correct
138
139 ```js
140 /* eslint wrap-iife: [2, "inside", { functionPrototypeMethods: true }] */
141
142 var x = (function(){ foo(); })()
143 var x = (function(){ foo(); }).call(bar)
144 ```
145
146 :::