Skip to content

Commit cd83eaa

Browse files
authored
docs: replace var with const in examples (#19530)
1 parent 7ff0cde commit cd83eaa

5 files changed

Lines changed: 77 additions & 76 deletions

File tree

docs/src/rules/func-name-matching.md

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ Examples of **incorrect** code for this rule:
1515
```js
1616
/*eslint func-name-matching: "error"*/
1717

18-
var foo = function bar() {};
18+
let foo = function bar() {};
1919
foo = function bar() {};
20+
const obj = {foo: function bar() {}};
2021
obj.foo = function bar() {};
2122
obj['foo'] = function bar() {};
22-
var obj = {foo: function bar() {}};
2323
({['foo']: function bar() {}});
2424

2525
class C {
@@ -34,11 +34,11 @@ class C {
3434
```js
3535
/*eslint func-name-matching: ["error", "never"] */
3636

37-
var foo = function foo() {};
37+
let foo = function foo() {};
3838
foo = function foo() {};
39+
const obj = {foo: function foo() {}};
3940
obj.foo = function foo() {};
4041
obj['foo'] = function foo() {};
41-
var obj = {foo: function foo() {}};
4242
({['foo']: function foo() {}});
4343

4444
class C {
@@ -56,23 +56,23 @@ Examples of **correct** code for this rule:
5656
/*eslint func-name-matching: "error"*/
5757
// equivalent to /*eslint func-name-matching: ["error", "always"]*/
5858

59-
var foo = function foo() {};
60-
var foo = function() {};
61-
var foo = () => {};
59+
const foo = function foo() {};
60+
const foo1 = function() {};
61+
const foo2 = () => {};
6262
foo = function foo() {};
6363

64+
const obj = {foo: function foo() {}};
6465
obj.foo = function foo() {};
6566
obj['foo'] = function foo() {};
6667
obj['foo//bar'] = function foo() {};
6768
obj[foo] = function bar() {};
6869

69-
var obj = {foo: function foo() {}};
70-
var obj = {[foo]: function bar() {}};
71-
var obj = {'foo//bar': function foo() {}};
72-
var obj = {foo: function() {}};
70+
const obj1 = {[foo]: function bar() {}};
71+
const obj2 = {'foo//bar': function foo() {}};
72+
const obj3 = {foo: function() {}};
7373

7474
obj['x' + 2] = function bar(){};
75-
var [ bar ] = [ function bar(){} ];
75+
const [ bar ] = [ function bar(){} ];
7676
({[foo]: function bar() {}})
7777

7878
class C {
@@ -101,23 +101,24 @@ module['exports'] = function foo(name) {};
101101
```js
102102
/*eslint func-name-matching: ["error", "never"] */
103103

104-
var foo = function bar() {};
105-
var foo = function() {};
106-
var foo = () => {};
104+
let foo = function bar() {};
105+
const foo1 = function() {};
106+
const foo2 = () => {};
107107
foo = function bar() {};
108108

109+
const obj = {foo: function bar() {}};
109110
obj.foo = function bar() {};
110111
obj['foo'] = function bar() {};
111112
obj['foo//bar'] = function foo() {};
112113
obj[foo] = function foo() {};
113114

114-
var obj = {foo: function bar() {}};
115-
var obj = {[foo]: function foo() {}};
116-
var obj = {'foo//bar': function foo() {}};
117-
var obj = {foo: function() {}};
115+
const obj1 = {foo: function bar() {}};
116+
const obj2 = {[foo]: function foo() {}};
117+
const obj3 = {'foo//bar': function foo() {}};
118+
const obj4 = {foo: function() {}};
118119

119120
obj['x' + 2] = function bar(){};
120-
var [ bar ] = [ function bar(){} ];
121+
const [ bar ] = [ function bar(){} ];
121122
({[foo]: function bar() {}})
122123

123124
class C {
@@ -156,7 +157,7 @@ Examples of **correct** code for the `{ considerPropertyDescriptor: true }` opti
156157
```js
157158
/*eslint func-name-matching: ["error", { "considerPropertyDescriptor": true }]*/
158159
// equivalent to /*eslint func-name-matching: ["error", "always", { "considerPropertyDescriptor": true }]*/
159-
var obj = {};
160+
const obj = {};
160161
Object.create(obj, {foo:{value: function foo() {}}});
161162
Object.defineProperty(obj, 'bar', {value: function bar() {}});
162163
Object.defineProperties(obj, {baz:{value: function baz() {} }});
@@ -172,7 +173,7 @@ Examples of **incorrect** code for the `{ considerPropertyDescriptor: true }` op
172173
```js
173174
/*eslint func-name-matching: ["error", { "considerPropertyDescriptor": true }]*/
174175
// equivalent to /*eslint func-name-matching: ["error", "always", { "considerPropertyDescriptor": true }]*/
175-
var obj = {};
176+
const obj = {};
176177
Object.create(obj, {foo:{value: function bar() {}}});
177178
Object.defineProperty(obj, 'bar', {value: function baz() {}});
178179
Object.defineProperties(obj, {baz:{value: function foo() {} }});

docs/src/rules/no-new-wrappers.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,29 @@ further_reading:
1212
There are three primitive types in JavaScript that have wrapper objects: string, number, and boolean. These are represented by the constructors `String`, `Number`, and `Boolean`, respectively. The primitive wrapper types are used whenever one of these primitive values is read, providing them with object-like capabilities such as methods. Behind the scenes, an object of the associated wrapper type is created and then destroyed, which is why you can call methods on primitive values, such as:
1313

1414
```js
15-
var text = "Hello world".substring(2);
15+
const text = "Hello world".substring(2);
1616
```
1717

1818
Behind the scenes in this example, a `String` object is constructed. The `substring()` method exists on `String.prototype` and so is accessible to the string instance.
1919

2020
It's also possible to manually create a new wrapper instance:
2121

2222
```js
23-
var stringObject = new String("Hello world");
24-
var numberObject = new Number(33);
25-
var booleanObject = new Boolean(false);
23+
const stringObject = new String("Hello world");
24+
const numberObject = new Number(33);
25+
const booleanObject = new Boolean(false);
2626
```
2727

2828
Although possible, there aren't any good reasons to use these primitive wrappers as constructors. They tend to confuse other developers more than anything else because they seem like they should act as primitives, but they do not. For example:
2929

3030
```js
31-
var stringObject = new String("Hello world");
31+
const stringObject = new String("Hello world");
3232
console.log(typeof stringObject); // "object"
3333

34-
var text = "Hello world";
34+
const text = "Hello world";
3535
console.log(typeof text); // "string"
3636

37-
var booleanObject = new Boolean(false);
37+
const booleanObject = new Boolean(false);
3838
if (booleanObject) { // all objects are truthy!
3939
console.log("This executes");
4040
}
@@ -55,13 +55,13 @@ Examples of **incorrect** code for this rule:
5555
```js
5656
/*eslint no-new-wrappers: "error"*/
5757

58-
var stringObject = new String("Hello world");
59-
var numberObject = new Number(33);
60-
var booleanObject = new Boolean(false);
58+
const stringObject = new String("Hello world");
59+
const numberObject = new Number(33);
60+
const booleanObject = new Boolean(false);
6161

62-
var stringObject = new String;
63-
var numberObject = new Number;
64-
var booleanObject = new Boolean;
62+
const stringObject2 = new String;
63+
const numberObject2 = new Number;
64+
const booleanObject2 = new Boolean;
6565
```
6666

6767
:::
@@ -73,10 +73,10 @@ Examples of **correct** code for this rule:
7373
```js
7474
/*eslint no-new-wrappers: "error"*/
7575

76-
var text = String(someValue);
77-
var num = Number(someValue);
76+
const text = String(someValue);
77+
const num = Number(someValue);
7878

79-
var object = new MyString();
79+
const object = new MyString();
8080
```
8181

8282
:::

docs/src/rules/no-nonoctal-decimal-escape.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ Examples of **incorrect** code for this rule:
3939

4040
"\9";
4141

42-
var foo = "w\8less";
42+
const foo = "w\8less";
4343

44-
var bar = "December 1\9";
44+
const bar = "December 1\9";
4545

46-
var baz = "Don't use \8 and \9 escapes.";
46+
const baz = "Don't use \8 and \9 escapes.";
4747

48-
var quux = "\0\8";
48+
const quux = "\0\8";
4949
```
5050

5151
:::
@@ -61,13 +61,13 @@ Examples of **correct** code for this rule:
6161

6262
"9";
6363

64-
var foo = "w8less";
64+
const foo = "w8less";
6565

66-
var bar = "December 19";
66+
const bar = "December 19";
6767

68-
var baz = "Don't use \\8 and \\9 escapes.";
68+
const baz = "Don't use \\8 and \\9 escapes.";
6969

70-
var quux = "\0\u0038";
70+
const quux = "\0\u0038";
7171
```
7272

7373
:::

docs/src/rules/no-octal.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ rule_type: suggestion
88
Octal literals are numerals that begin with a leading zero, such as:
99

1010
```js
11-
var num = 071; // 57
11+
const num = 071; // 57
1212
```
1313

1414
Because the leading zero which identifies an octal literal has been a source of confusion and error in JavaScript code, ECMAScript 5 deprecates the use of octal numeric literals.
@@ -26,8 +26,8 @@ Examples of **incorrect** code for this rule:
2626
```js
2727
/*eslint no-octal: "error"*/
2828

29-
var num = 071;
30-
var result = 5 + 07;
29+
const num = 071;
30+
const result = 5 + 07;
3131
```
3232

3333
:::
@@ -39,7 +39,7 @@ Examples of **correct** code for this rule:
3939
```js
4040
/*eslint no-octal: "error"*/
4141

42-
var num = "071";
42+
const num = "071";
4343
```
4444

4545
:::

docs/src/rules/no-param-reassign.md

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@ Examples of **incorrect** code for this rule:
2121
```js
2222
/*eslint no-param-reassign: "error"*/
2323

24-
var foo = function(bar) {
24+
const foo = function(bar) {
2525
bar = 13;
2626
}
2727

28-
var foo = function(bar) {
28+
const foo1 = function(bar) {
2929
bar++;
3030
}
3131

32-
var foo = function(bar) {
32+
const foo2 = function(bar) {
3333
for (bar in baz) {}
3434
}
3535

36-
var foo = function(bar) {
36+
const foo3 = function(bar) {
3737
for (bar of baz) {}
3838
}
3939
```
@@ -47,8 +47,8 @@ Examples of **correct** code for this rule:
4747
```js
4848
/*eslint no-param-reassign: "error"*/
4949

50-
var foo = function(bar) {
51-
var baz = bar;
50+
const foo = function(bar) {
51+
const baz = bar;
5252
}
5353
```
5454

@@ -67,23 +67,23 @@ Examples of **correct** code for the default `{ "props": false }` option:
6767
```js
6868
/*eslint no-param-reassign: ["error", { "props": false }]*/
6969

70-
var foo = function(bar) {
70+
const foo = function(bar) {
7171
bar.prop = "value";
7272
}
7373

74-
var foo = function(bar) {
74+
const foo1 = function(bar) {
7575
delete bar.aaa;
7676
}
7777

78-
var foo = function(bar) {
78+
const foo2 = function(bar) {
7979
bar.aaa++;
8080
}
8181

82-
var foo = function(bar) {
82+
const foo3 = function(bar) {
8383
for (bar.aaa in baz) {}
8484
}
8585

86-
var foo = function(bar) {
86+
const foo4 = function(bar) {
8787
for (bar.aaa of baz) {}
8888
}
8989
```
@@ -97,23 +97,23 @@ Examples of **incorrect** code for the `{ "props": true }` option:
9797
```js
9898
/*eslint no-param-reassign: ["error", { "props": true }]*/
9999

100-
var foo = function(bar) {
100+
const foo = function(bar) {
101101
bar.prop = "value";
102102
}
103103

104-
var foo = function(bar) {
104+
const foo1 = function(bar) {
105105
delete bar.aaa;
106106
}
107107

108-
var foo = function(bar) {
108+
const foo2 = function(bar) {
109109
bar.aaa++;
110110
}
111111

112-
var foo = function(bar) {
112+
const foo3 = function(bar) {
113113
for (bar.aaa in baz) {}
114114
}
115115

116-
var foo = function(bar) {
116+
const foo4 = function(bar) {
117117
for (bar.aaa of baz) {}
118118
}
119119
```
@@ -127,23 +127,23 @@ Examples of **correct** code for the `{ "props": true }` option with `"ignorePro
127127
```js
128128
/*eslint no-param-reassign: ["error", { "props": true, "ignorePropertyModificationsFor": ["bar"] }]*/
129129

130-
var foo = function(bar) {
130+
const foo = function(bar) {
131131
bar.prop = "value";
132132
}
133133

134-
var foo = function(bar) {
134+
const foo1 = function(bar) {
135135
delete bar.aaa;
136136
}
137137

138-
var foo = function(bar) {
138+
const foo2 = function(bar) {
139139
bar.aaa++;
140140
}
141141

142-
var foo = function(bar) {
142+
const foo3 = function(bar) {
143143
for (bar.aaa in baz) {}
144144
}
145145

146-
var foo = function(bar) {
146+
const foo4 = function(bar) {
147147
for (bar.aaa of baz) {}
148148
}
149149
```
@@ -157,23 +157,23 @@ Examples of **correct** code for the `{ "props": true }` option with `"ignorePro
157157
```js
158158
/*eslint no-param-reassign: ["error", { "props": true, "ignorePropertyModificationsForRegex": ["^bar"] }]*/
159159

160-
var foo = function(barVar) {
160+
const foo = function(barVar) {
161161
barVar.prop = "value";
162162
}
163163

164-
var foo = function(barrito) {
164+
const foo1 = function(barrito) {
165165
delete barrito.aaa;
166166
}
167167

168-
var foo = function(bar_) {
168+
const foo2 = function(bar_) {
169169
bar_.aaa++;
170170
}
171171

172-
var foo = function(barBaz) {
172+
const foo3 = function(barBaz) {
173173
for (barBaz.aaa in baz) {}
174174
}
175175

176-
var foo = function(barBaz) {
176+
const foo4 = function(barBaz) {
177177
for (barBaz.aaa of baz) {}
178178
}
179179
```

0 commit comments

Comments
 (0)