|
1 | | -# disallow variable and `function` declarations in the global scope (no-implicit-globals) |
| 1 | +# Disallow declarations in the global scope (no-implicit-globals) |
2 | 2 |
|
3 | | -When working with browser scripts, developers often forget that variable and function declarations at the top-level scope become global variables on the `window` object. As opposed to modules which have their own scope. Globals should be explicitly assigned to `window` or `self` if that is the intent. Otherwise variables intended to be local to the script should be wrapped in an IIFE. |
| 3 | +It is the best practice to avoid 'polluting' the global scope with variables that are intended to be local to the script. |
| 4 | + |
| 5 | +Global variables created from a script can produce name collisions with global variables created from another script, which will |
| 6 | +usually lead to runtime errors or unexpected behavior. |
| 7 | + |
| 8 | +This rule disallows the following: |
| 9 | + |
| 10 | +* Declarations that create one or more variables in the global scope. |
| 11 | +* Global variable leaks. |
| 12 | +* Redeclarations of read-only global variables and assignments to read-only global variables. |
| 13 | + |
| 14 | +There is an explicit way to create a global variable when needed, by assigning to a property of the global object. |
| 15 | + |
| 16 | +This rule is mostly useful for browser scripts. Top-level declarations in ES modules and CommonJS modules create module-scoped |
| 17 | +variables. ES modules also have implicit `strict` mode, which prevents global variable leaks. |
| 18 | + |
| 19 | +By default, this rule does not check `const`, `let` and `class` declarations. |
| 20 | + |
| 21 | +This rule has an object option with one option: |
| 22 | + |
| 23 | +* Set `"lexicalBindings"` to `true` if you want this rule to check `const`, `let` and `class` declarations as well. |
4 | 24 |
|
5 | 25 | ## Rule Details |
6 | 26 |
|
7 | | -This rule disallows `var` and named `function` declarations at the top-level script scope. This does not apply to ES and CommonJS modules since they have a module scope. |
| 27 | +### `var` and `function` declarations |
| 28 | + |
| 29 | +When working with browser scripts, developers often forget that variable and function declarations at the top-level scope become global variables on the `window` object. As opposed to modules which have their own scope. Globals should be explicitly assigned to `window` or `self` if that is the intent. Otherwise variables intended to be local to the script should be wrapped in an IIFE. |
| 30 | + |
| 31 | +This rule disallows `var` and `function` declarations at the top-level script scope. This does not apply to ES and CommonJS modules since they have a module scope. |
8 | 32 |
|
9 | 33 | Examples of **incorrect** code for this rule: |
10 | 34 |
|
@@ -43,10 +67,160 @@ var foo = 1; |
43 | 67 | function bar() {} |
44 | 68 | ``` |
45 | 69 |
|
| 70 | +### Global variable leaks |
| 71 | + |
| 72 | +When the code is not in `strict` mode, an assignment to an undeclared variable creates |
| 73 | +a new global variable. This will happen even is the code is in a function. |
| 74 | + |
| 75 | +This does not apply to ES modules since the module code is implicitly in `strict` mode. |
| 76 | + |
| 77 | +Examples of **incorrect** code for this rule: |
| 78 | + |
| 79 | +```js |
| 80 | +/*eslint no-implicit-globals: "error"*/ |
| 81 | + |
| 82 | +foo = 1; |
| 83 | + |
| 84 | +Bar.prototype.baz = function () { |
| 85 | + a = 1; // Intended to be this.a = 1; |
| 86 | +}; |
| 87 | +``` |
| 88 | + |
| 89 | +### Read-only global variables |
| 90 | + |
| 91 | +This rule also disallows redeclarations of read-only global variables and assigments to read-only global variables. |
| 92 | + |
| 93 | +A read-only global variable can be a built-in ES global (e.g. `Array`), an environment specific global |
| 94 | +(e.g. `window` in the browser environment), or a global variable defined as `readonly` in the configuration file |
| 95 | +or in a `/*global */` comment. |
| 96 | + |
| 97 | +* [Specifying Environments](../user-guide/configuring#specifying-environments) |
| 98 | +* [Specifying Globals](../user-guide/configuring#specifying-globals) |
| 99 | + |
| 100 | +Examples of **incorrect** code for this rule: |
| 101 | + |
| 102 | +```js |
| 103 | +/*eslint no-implicit-globals: "error"*/ |
| 104 | + |
| 105 | +/*global foo:readonly*/ |
| 106 | + |
| 107 | +foo = 1; |
| 108 | + |
| 109 | +Array = []; |
| 110 | +var Object; |
| 111 | +``` |
| 112 | + |
| 113 | +### `const`, `let` and `class` declarations |
| 114 | + |
| 115 | +Lexical declarations `const` and `let`, as well as `class` declarations, create variables that are block-scoped. |
| 116 | + |
| 117 | +However, when declared in the top-level of a browser script these variables are not 'script-scoped'. |
| 118 | +They are actually created in the global scope and could produce name collisions with |
| 119 | +`var`, `const` and `let` variables and `function` and `class` declarations from other scripts. |
| 120 | +This does not apply to ES and CommonJS modules. |
| 121 | + |
| 122 | +If the variable is intended to be local to the script, wrap the code with a block or with an immediately-invoked function expression (IIFE). |
| 123 | + |
| 124 | +Examples of **correct** code for this rule with `"lexicalBindings"` option set to `false` (default): |
| 125 | + |
| 126 | +```js |
| 127 | +/*eslint no-implicit-globals: ["error", {"lexicalBindings": false}]*/ |
| 128 | + |
| 129 | +const foo = 1; |
| 130 | + |
| 131 | +let baz; |
| 132 | + |
| 133 | +class Bar {} |
| 134 | +``` |
| 135 | + |
| 136 | +Examples of **incorrect** code for this rule with `"lexicalBindings"` option set to `true`: |
| 137 | + |
| 138 | +```js |
| 139 | +/*eslint no-implicit-globals: ["error", {"lexicalBindings": true}]*/ |
| 140 | + |
| 141 | +const foo = 1; |
| 142 | + |
| 143 | +let baz; |
| 144 | + |
| 145 | +class Bar {} |
| 146 | +``` |
| 147 | + |
| 148 | +Examples of **correct** code for this rule with `"lexicalBindings"` option set to `true`: |
| 149 | + |
| 150 | +```js |
| 151 | +/*eslint no-implicit-globals: ["error", {"lexicalBindings": true}]*/ |
| 152 | + |
| 153 | +{ |
| 154 | + const foo = 1; |
| 155 | + let baz; |
| 156 | + class Bar {} |
| 157 | +} |
| 158 | + |
| 159 | +(function() { |
| 160 | + const foo = 1; |
| 161 | + let baz; |
| 162 | + class Bar {} |
| 163 | +}()); |
| 164 | +``` |
| 165 | + |
| 166 | +If you intend to create a global `const` or `let` variable or a global `class` declaration, to be used from other scripts, |
| 167 | +be aware that there are certain differences when compared to the traditional methods, which are `var` declarations and assigning to a property of the global `window` object: |
| 168 | + |
| 169 | +* Lexically declared variables cannot be conditionally created. A script cannot check for the existence of |
| 170 | +a variable and then create a new one. `var` variables are also always created, but redeclarations do not |
| 171 | +cause runtime exceptions. |
| 172 | +* Lexically declared variables do not create properties on the global object, which is what a consuming script might expect. |
| 173 | +* Lexically declared variables are shadowing properties of the global object, which might produce errors if a |
| 174 | +consuming script is using both the variable and the property. |
| 175 | +* Lexically declared variables can produce a permanent Temporal Dead Zone (TDZ) if the initialization throws an exception. |
| 176 | +Even the `typeof` check is not safe from TDZ reference exceptions. |
| 177 | + |
| 178 | +Examples of **incorrect** code for this rule with `"lexicalBindings"` option set to `true`: |
| 179 | + |
| 180 | +```js |
| 181 | +/*eslint no-implicit-globals: ["error", {"lexicalBindings": true}]*/ |
| 182 | + |
| 183 | +const MyGobalFunction = (function() { |
| 184 | + const a = 1; |
| 185 | + let b = 2; |
| 186 | + return function() { |
| 187 | + return a + b; |
| 188 | + } |
| 189 | +}()); |
| 190 | +``` |
| 191 | + |
| 192 | +Examples of **correct** code for this rule with `"lexicalBindings"` option set to `true`: |
| 193 | + |
| 194 | +```js |
| 195 | +/*eslint no-implicit-globals: ["error", {"lexicalBindings": true}]*/ |
| 196 | + |
| 197 | +window.MyGobalFunction = (function() { |
| 198 | + const a = 1; |
| 199 | + let b = 2; |
| 200 | + return function() { |
| 201 | + return a + b; |
| 202 | + } |
| 203 | +}()); |
| 204 | +``` |
| 205 | + |
46 | 206 | ## When Not To Use It |
47 | 207 |
|
48 | | -If you want to be able to declare variables and functions in the global scope you can safely disable this rule. Or if you are always using module scoped files, this rule will never apply. |
| 208 | +In the case of a browser script, if you want to be able to explicitly declare variables and functions in the global scope, |
| 209 | +and your code is in strict mode or you don't want this rule to warn you about undeclared variables, |
| 210 | +and you also don't want this rule to warn you about read-only globals, you can disable this rule. |
| 211 | + |
| 212 | +In the case of a CommonJS module, if your code is in strict mode or you don't want this rule to warn you about undeclared variables, |
| 213 | +and you also don't want this rule to warn you about the read-only globals, you can disable this rule. |
| 214 | + |
| 215 | +In the case of an ES module, if you don't want this rule to warn you about the read-only globals you can disable this rule. |
49 | 216 |
|
50 | 217 | ## Further Reading |
51 | 218 |
|
52 | 219 | * [Immediately-Invoked Function Expression (IIFE)](http://benalman.com/news/2010/11/immediately-invoked-function-expression/) |
| 220 | +* [ReferenceError: assignment to undeclared variable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Undeclared_var) |
| 221 | +* [Temporal Dead Zone](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let#Temporal_dead_zone) |
| 222 | + |
| 223 | +## Related Rules |
| 224 | + |
| 225 | +* [no-undef](no-undef.md) |
| 226 | +* [no-global-assign](no-global-assign.md) |
0 commit comments