]>
Commit | Line | Data |
---|---|---|
8f9d1d4d DC |
1 | --- |
2 | title: no-native-reassign | |
8f9d1d4d DC |
3 | rule_type: suggestion |
4 | related_rules: | |
5 | - no-extend-native | |
6 | - no-redeclare | |
7 | - no-shadow | |
8 | --- | |
eb39fafa | 9 | |
8f9d1d4d DC |
10 | |
11 | This rule was **deprecated** in ESLint v3.3.0 and replaced by the [no-global-assign](no-global-assign) rule. | |
eb39fafa DC |
12 | |
13 | 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: | |
14 | ||
15 | ```js | |
16 | window = {}; | |
17 | ``` | |
18 | ||
19 | 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. | |
20 | ||
21 | ## Rule Details | |
22 | ||
23 | This rule disallows modifications to read-only global variables. | |
24 | ||
25 | ESLint 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 | |
30 | Examples of **incorrect** code for this rule: | |
31 | ||
8f9d1d4d DC |
32 | ::: incorrect |
33 | ||
eb39fafa DC |
34 | ```js |
35 | /*eslint no-native-reassign: "error"*/ | |
36 | ||
37 | Object = null | |
38 | undefined = 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 | ||
49 | window = {} | |
50 | length = 1 | |
51 | top = 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 | ||
62 | a = 1 | |
63 | ``` | |
64 | ||
8f9d1d4d DC |
65 | ::: |
66 | ||
eb39fafa DC |
67 | Examples of **correct** code for this rule: |
68 | ||
8f9d1d4d DC |
69 | ::: correct |
70 | ||
eb39fafa DC |
71 | ```js |
72 | /*eslint no-native-reassign: "error"*/ | |
73 | ||
74 | a = 1 | |
75 | var b = 1 | |
76 | b = 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 | ||
87 | onload = 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 | ||
98 | a = 1 | |
99 | ``` | |
100 | ||
8f9d1d4d DC |
101 | ::: |
102 | ||
eb39fafa DC |
103 | ## Options |
104 | ||
105 | This 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 | ||
117 | If you are trying to override one of the native objects. |