]>
Commit | Line | Data |
---|---|---|
8f9d1d4d DC |
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 | --- | |
eb39fafa | 10 | |
8f9d1d4d DC |
11 | |
12 | This rule was **deprecated** in ESLint v3.3.0 and replaced by the [no-global-assign](no-global-assign) rule. | |
eb39fafa DC |
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 | ||
8f9d1d4d DC |
33 | ::: incorrect |
34 | ||
eb39fafa DC |
35 | ```js |
36 | /*eslint no-native-reassign: "error"*/ | |
37 | ||
38 | Object = null | |
39 | undefined = 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 | ||
50 | window = {} | |
51 | length = 1 | |
52 | top = 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 | ||
63 | a = 1 | |
64 | ``` | |
65 | ||
8f9d1d4d DC |
66 | ::: |
67 | ||
eb39fafa DC |
68 | Examples of **correct** code for this rule: |
69 | ||
8f9d1d4d DC |
70 | ::: correct |
71 | ||
eb39fafa DC |
72 | ```js |
73 | /*eslint no-native-reassign: "error"*/ | |
74 | ||
75 | a = 1 | |
76 | var b = 1 | |
77 | b = 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 | ||
88 | onload = 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 | ||
99 | a = 1 | |
100 | ``` | |
101 | ||
8f9d1d4d DC |
102 | ::: |
103 | ||
eb39fafa DC |
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. |