]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/no-native-reassign.md
build: add missing dh-nodejs to build-dependencies
[pve-eslint.git] / eslint / docs / src / rules / no-native-reassign.md
1 ---
2 title: no-native-reassign
3 layout: doc
4 rule_type: suggestion
5 related_rules:
6 - no-extend-native
7 - no-redeclare
8 - no-shadow
9 ---
10
11
12 This rule was **deprecated** in ESLint v3.3.0 and replaced by the [no-global-assign](no-global-assign) rule.
13
14 JavaScript 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:
15
16 ```js
17 window = {};
18 ```
19
20 While 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.
21
22 ## Rule Details
23
24 This rule disallows modifications to read-only global variables.
25
26 ESLint has the capability to configure global variables as read-only.
27
28 * [Specifying Environments](../user-guide/configuring#specifying-environments)
29 * [Specifying Globals](../user-guide/configuring#specifying-globals)
30
31 Examples of **incorrect** code for this rule:
32
33 ::: incorrect
34
35 ```js
36 /*eslint no-native-reassign: "error"*/
37
38 Object = null
39 undefined = 1
40 ```
41
42 :::
43
44 ::: incorrect
45
46 ```js
47 /*eslint no-native-reassign: "error"*/
48 /*eslint-env browser*/
49
50 window = {}
51 length = 1
52 top = 1
53 ```
54
55 :::
56
57 ::: incorrect
58
59 ```js
60 /*eslint no-native-reassign: "error"*/
61 /*global a:readonly*/
62
63 a = 1
64 ```
65
66 :::
67
68 Examples of **correct** code for this rule:
69
70 ::: correct
71
72 ```js
73 /*eslint no-native-reassign: "error"*/
74
75 a = 1
76 var b = 1
77 b = 2
78 ```
79
80 :::
81
82 ::: correct
83
84 ```js
85 /*eslint no-native-reassign: "error"*/
86 /*eslint-env browser*/
87
88 onload = function() {}
89 ```
90
91 :::
92
93 ::: correct
94
95 ```js
96 /*eslint no-native-reassign: "error"*/
97 /*global a:writable*/
98
99 a = 1
100 ```
101
102 :::
103
104 ## Options
105
106 This rule accepts an `exceptions` option, which can be used to specify a list of builtins for which reassignments will be allowed:
107
108 ```json
109 {
110 "rules": {
111 "no-native-reassign": ["error", {"exceptions": ["Object"]}]
112 }
113 }
114 ```
115
116 ## When Not To Use It
117
118 If you are trying to override one of the native objects.