]> git.proxmox.com Git - pve-eslint.git/blame - 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
CommitLineData
8f9d1d4d
DC
1---
2title: no-native-reassign
3layout: doc
4rule_type: suggestion
5related_rules:
6- no-extend-native
7- no-redeclare
8- no-shadow
9---
eb39fafa 10
8f9d1d4d
DC
11
12This rule was **deprecated** in ESLint v3.3.0 and replaced by the [no-global-assign](no-global-assign) rule.
eb39fafa
DC
13
14JavaScript 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
17window = {};
18```
19
20While 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
24This rule disallows modifications to read-only global variables.
25
26ESLint 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
31Examples of **incorrect** code for this rule:
32
8f9d1d4d
DC
33::: incorrect
34
eb39fafa
DC
35```js
36/*eslint no-native-reassign: "error"*/
37
38Object = null
39undefined = 1
40```
41
8f9d1d4d
DC
42:::
43
44::: incorrect
45
eb39fafa
DC
46```js
47/*eslint no-native-reassign: "error"*/
48/*eslint-env browser*/
49
50window = {}
51length = 1
52top = 1
53```
54
8f9d1d4d
DC
55:::
56
57::: incorrect
58
eb39fafa
DC
59```js
60/*eslint no-native-reassign: "error"*/
61/*global a:readonly*/
62
63a = 1
64```
65
8f9d1d4d
DC
66:::
67
eb39fafa
DC
68Examples of **correct** code for this rule:
69
8f9d1d4d
DC
70::: correct
71
eb39fafa
DC
72```js
73/*eslint no-native-reassign: "error"*/
74
75a = 1
76var b = 1
77b = 2
78```
79
8f9d1d4d
DC
80:::
81
82::: correct
83
eb39fafa
DC
84```js
85/*eslint no-native-reassign: "error"*/
86/*eslint-env browser*/
87
88onload = function() {}
89```
90
8f9d1d4d
DC
91:::
92
93::: correct
94
eb39fafa
DC
95```js
96/*eslint no-native-reassign: "error"*/
97/*global a:writable*/
98
99a = 1
100```
101
8f9d1d4d
DC
102:::
103
eb39fafa
DC
104## Options
105
106This 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
118If you are trying to override one of the native objects.