]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/src/rules/no-native-reassign.md
import 8.41.0 source
[pve-eslint.git] / eslint / docs / src / rules / no-native-reassign.md
CommitLineData
8f9d1d4d
DC
1---
2title: no-native-reassign
8f9d1d4d
DC
3rule_type: suggestion
4related_rules:
5- no-extend-native
6- no-redeclare
7- no-shadow
8---
eb39fafa 9
8f9d1d4d
DC
10
11This rule was **deprecated** in ESLint v3.3.0 and replaced by the [no-global-assign](no-global-assign) rule.
eb39fafa
DC
12
13JavaScript environments contain a number of built-in global variables, such as `window` in browsers and `process` in Node.js. In almost all cases, you don't want to assign a value to these global variables as doing so could result in losing access to important functionality. For example, you probably don't want to do this in browser code:
14
15```js
16window = {};
17```
18
19While examples such as `window` are obvious, there are often hundreds of built-in global objects provided by JavaScript environments. It can be hard to know if you're assigning to a global variable or not.
20
21## Rule Details
22
23This rule disallows modifications to read-only global variables.
24
25ESLint has the capability to configure global variables as read-only.
26
f2a92ac6
DC
27* [Specifying Environments](../use/configure#specifying-environments)
28* [Specifying Globals](../use/configure#specifying-globals)
eb39fafa
DC
29
30Examples of **incorrect** code for this rule:
31
8f9d1d4d
DC
32::: incorrect
33
eb39fafa
DC
34```js
35/*eslint no-native-reassign: "error"*/
36
37Object = null
38undefined = 1
39```
40
8f9d1d4d
DC
41:::
42
43::: incorrect
44
eb39fafa
DC
45```js
46/*eslint no-native-reassign: "error"*/
47/*eslint-env browser*/
48
49window = {}
50length = 1
51top = 1
52```
53
8f9d1d4d
DC
54:::
55
56::: incorrect
57
eb39fafa
DC
58```js
59/*eslint no-native-reassign: "error"*/
60/*global a:readonly*/
61
62a = 1
63```
64
8f9d1d4d
DC
65:::
66
eb39fafa
DC
67Examples of **correct** code for this rule:
68
8f9d1d4d
DC
69::: correct
70
eb39fafa
DC
71```js
72/*eslint no-native-reassign: "error"*/
73
74a = 1
75var b = 1
76b = 2
77```
78
8f9d1d4d
DC
79:::
80
81::: correct
82
eb39fafa
DC
83```js
84/*eslint no-native-reassign: "error"*/
85/*eslint-env browser*/
86
87onload = function() {}
88```
89
8f9d1d4d
DC
90:::
91
92::: correct
93
eb39fafa
DC
94```js
95/*eslint no-native-reassign: "error"*/
96/*global a:writable*/
97
98a = 1
99```
100
8f9d1d4d
DC
101:::
102
eb39fafa
DC
103## Options
104
105This rule accepts an `exceptions` option, which can be used to specify a list of builtins for which reassignments will be allowed:
106
107```json
108{
109 "rules": {
110 "no-native-reassign": ["error", {"exceptions": ["Object"]}]
111 }
112}
113```
114
115## When Not To Use It
116
117If you are trying to override one of the native objects.