Skip to content

Commit e65d363

Browse files
benjamingrrvagg
authored andcommitted
doc: replace function expressions with arrows
This commit replaces multiple usages of `function(){}` with ES2015 arrow functions in places it was forgotten earlier. The goal is to make the docs more consistent since other functions were already replaced with ES2015 arrows. In addition, it fixes invalid syntax in modules.markdown to valid syntax as well as remove `var self = this` pattern usages in the code where they are now possible to avoid through arrow functions. PR-URL: #4832 Reviewed-By: Roman Reiss <[email protected]> Reviewed-By: Сковорода Никита Андреевич <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 423a58d commit e65d363

13 files changed

+47
-47
lines changed

doc/api/addons.markdown

+4-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ be used as a starting-point for your own Addon.
4444
This "Hello world" example is a simple Addon, written in C++, that is the
4545
equivalent of the following JavaScript code:
4646

47-
module.exports.hello = function() { return 'world'; };
47+
```js
48+
module.exports.hello = () => 'world';
49+
```
4850

4951
First, create the file `hello.cc`:
5052

@@ -368,7 +370,7 @@ To test it, run the following JavaScript:
368370
// test.js
369371
const addon = require('./build/Release/addon');
370372
371-
addon(function(msg){
373+
addon((msg) => {
372374
console.log(msg); // 'hello world'
373375
});
374376
```

doc/api/assert.markdown

+6-6
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ matching error type in the assertion:
139139

140140
```js
141141
assert.doesNotThrow(
142-
function() {
142+
() => {
143143
throw new TypeError('Wrong value');
144144
},
145145
SyntaxError
@@ -151,7 +151,7 @@ However, the following will result in an `AssertionError` with the message
151151

152152
```js
153153
assert.doesNotThrow(
154-
function() {
154+
() => {
155155
throw new TypeError('Wrong value');
156156
},
157157
TypeError
@@ -164,7 +164,7 @@ message:
164164

165165
```js
166166
assert.doesNotThrow(
167-
function() {
167+
() => {
168168
throw new TypeError('Wrong value');
169169
},
170170
TypeError,
@@ -359,7 +359,7 @@ Validate instanceof using constructor:
359359

360360
```js
361361
assert.throws(
362-
function() {
362+
() => {
363363
throw new Error('Wrong value');
364364
},
365365
Error
@@ -370,7 +370,7 @@ Validate error message using [`RegExp`][]:
370370

371371
```js
372372
assert.throws(
373-
function() {
373+
() => {
374374
throw new Error('Wrong value');
375375
},
376376
/value/
@@ -381,7 +381,7 @@ Custom error validation:
381381

382382
```js
383383
assert.throws(
384-
function() {
384+
() => {
385385
throw new Error('Wrong value');
386386
},
387387
function(err) {

doc/api/debugger.markdown

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ $ node debug myscript.js
1515
connecting... ok
1616
break in /home/indutny/Code/git/indutny/myscript.js:1
1717
1 x = 5;
18-
2 setTimeout(function () {
18+
2 setTimeout(() => {
1919
3 debugger;
2020
debug>
2121
```
@@ -31,7 +31,7 @@ For example, suppose `myscript.js` is written as:
3131
```js
3232
// myscript.js
3333
x = 5;
34-
setTimeout(function () {
34+
setTimeout(() => {
3535
debugger;
3636
console.log('world');
3737
}, 1000);
@@ -46,19 +46,19 @@ $ node debug myscript.js
4646
connecting... ok
4747
break in /home/indutny/Code/git/indutny/myscript.js:1
4848
1 x = 5;
49-
2 setTimeout(function () {
49+
2 setTimeout(() => {
5050
3 debugger;
5151
debug> cont
5252
< hello
5353
break in /home/indutny/Code/git/indutny/myscript.js:3
5454
1 x = 5;
55-
2 setTimeout(function () {
55+
2 setTimeout(() => {
5656
3 debugger;
5757
4 console.log('world');
5858
5 }, 1000);
5959
debug> next
6060
break in /home/indutny/Code/git/indutny/myscript.js:4
61-
2 setTimeout(function () {
61+
2 setTimeout(() => {
6262
3 debugger;
6363
4 console.log('world');
6464
5 }, 1000);
@@ -135,7 +135,7 @@ Warning: script 'mod.js' was not loaded yet.
135135
debug> c
136136
break in test/fixtures/break-in-module/mod.js:23
137137
21
138-
22 exports.hello = function() {
138+
22 exports.hello = () => {
139139
23 return 'hello from module';
140140
24 };
141141
25

doc/api/domain.markdown

+2-2
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ thrown will be routed to the domain's `'error'` event.
349349
const d = domain.create();
350350

351351
function readSomeFile(filename, cb) {
352-
fs.readFile(filename, 'utf8', d.bind(function(er, data) {
352+
fs.readFile(filename, 'utf8', d.bind((er, data) => {
353353
// if this throws, it will also be passed to the domain
354354
return cb(er, data ? JSON.parse(data) : null);
355355
}));
@@ -380,7 +380,7 @@ with a single error handler in a single place.
380380
const d = domain.create();
381381

382382
function readSomeFile(filename, cb) {
383-
fs.readFile(filename, 'utf8', d.intercept(function(data) {
383+
fs.readFile(filename, 'utf8', d.intercept((data) => {
384384
// note, the first argument is never passed to the
385385
// callback since it is assumed to be the 'Error' argument
386386
// and thus intercepted by the domain.

doc/api/events.markdown

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function MyEmitter() {
3737
util.inherits(MyEmitter, EventEmitter);
3838

3939
const myEmitter = new MyEmitter();
40-
myEmitter.on('event', function() {
40+
myEmitter.on('event', () => {
4141
console.log('an event occurred!');
4242
});
4343
myEmitter.emit('event');
@@ -54,7 +54,7 @@ const EventEmitter = require('events');
5454
class MyEmitter extends EventEmitter {}
5555

5656
const myEmitter = new MyEmitter();
57-
myEmitter.on('event', function() {
57+
myEmitter.on('event', () => {
5858
console.log('an event occurred!');
5959
});
6060
myEmitter.emit('event');
@@ -364,7 +364,7 @@ Removes the specified `listener` from the listener array for the specified
364364
`event`.
365365

366366
```js
367-
var callback = function(stream) {
367+
var callback = (stream) => {
368368
console.log('someone connected!');
369369
};
370370
server.on('connection', callback);

doc/api/fs.markdown

+1-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ argument will be populated. The following example checks if the file
239239
`/etc/passwd` can be read and written by the current process.
240240

241241
```js
242-
fs.access('/etc/passwd', fs.R_OK | fs.W_OK, function (err) {
242+
fs.access('/etc/passwd', fs.R_OK | fs.W_OK, (err) => {
243243
console.log(err ? 'no access!' : 'can read/write');
244244
});
245245
```

doc/api/modules.markdown

+7-12
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,10 @@ The contents of `circle.js`:
2020
```js
2121
const PI = Math.PI;
2222

23-
exports.area = function (r) {
24-
return PI * r * r;
25-
};
23+
exports.area = (r) => PI * r * r;
24+
25+
exports.circumference = (r) => 2 * PI * r;
2626

27-
exports.circumference = function (r) {
28-
return 2 * PI * r;
29-
};
3027
```
3128

3229
The module `circle.js` has exported the functions `area()` and
@@ -53,11 +50,9 @@ The `square` module is defined in `square.js`:
5350

5451
```js
5552
// assigning to exports will not modify module, must use module.exports
56-
module.exports = function(width) {
53+
module.exports = (width) => {
5754
return {
58-
area: function() {
59-
return width * width;
60-
}
55+
area: () => width * width
6156
};
6257
}
6358
```
@@ -498,12 +493,12 @@ To illustrate the behavior, imagine this hypothetical implementation of
498493
```js
499494
function require(...) {
500495
// ...
501-
function (module, exports) {
496+
((module, exports) => {
502497
// Your module code here
503498
exports = some_func; // re-assigns exports, exports is no longer
504499
// a shortcut, and nothing is exported.
505500
module.exports = some_func; // makes your module export 0
506-
} (module, module.exports);
501+
})(module, module.exports);
507502
return module;
508503
}
509504
```

doc/api/process.markdown

+3-3
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ var resource = new SomeResource();
175175

176176
In cases like this, you may not want to track the rejection as a developer
177177
error like you would for other `'unhandledRejection'` events. To address
178-
this, you can either attach a dummy `.catch(function() { })` handler to
178+
this, you can either attach a dummy `.catch(() => { })` handler to
179179
`resource.loaded`, preventing the `'unhandledRejection'` event from being
180180
emitted, or you can use the [`'rejectionHandled'`][] event.
181181

@@ -720,7 +720,7 @@ function maybeSync(arg, cb) {
720720
This API is hazardous. If you do this:
721721

722722
```js
723-
maybeSync(true, function() {
723+
maybeSync(true, () => {
724724
foo();
725725
});
726726
bar();
@@ -954,7 +954,7 @@ A `Writable Stream` to `stdout` (on fd `1`).
954954
For example, a `console.log` equivalent could look like this:
955955

956956
```js
957-
console.log = function(msg) {
957+
console.log = (msg) => {
958958
process.stdout.write(`${msg}\n`);
959959
};
960960
```

doc/api/readline.markdown

+1-1
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ const rl = readline.createInterface({
265265
input: fs.createReadStream('sample.txt')
266266
});
267267

268-
rl.on('line', function (line) {
268+
rl.on('line', (line) => {
269269
console.log('Line from file:', line);
270270
});
271271
```

doc/api/repl.markdown

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ $ node
1515
Type '.help' for options.
1616
> a = [ 1, 2, 3];
1717
[ 1, 2, 3 ]
18-
> a.forEach(function (v){
18+
> a.forEach((v) => {
1919
... console.log(v);
2020
... });
2121
1
@@ -139,7 +139,7 @@ For example, if you have defined an `inspect()` function on an object, like this
139139
```
140140
> var obj = {foo: 'this will not show up in the inspect() output'};
141141
undefined
142-
> obj.inspect = function() {
142+
> obj.inspect = () => {
143143
... return {bar: 'baz'};
144144
... };
145145
[Function]

doc/api/stream.markdown

+5-6
Original file line numberDiff line numberDiff line change
@@ -925,18 +925,17 @@ function SourceWrapper(options) {
925925
Readable.call(this, options);
926926

927927
this._source = getLowlevelSourceObject();
928-
var self = this;
929928

930929
// Every time there's data, we push it into the internal buffer.
931-
this._source.ondata = function(chunk) {
930+
this._source.ondata = (chunk) => {
932931
// if push() returns false, then we need to stop reading from source
933-
if (!self.push(chunk))
934-
self._source.readStop();
932+
if (!this.push(chunk))
933+
this._source.readStop();
935934
};
936935

937936
// When the source ends, we push the EOF-signaling `null` chunk
938-
this._source.onend = function() {
939-
self.push(null);
937+
this._source.onend = () => {
938+
this.push(null);
940939
};
941940
}
942941

doc/api/util.markdown

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ Marks that a method should not be used any more.
5858
```js
5959
const util = require('util');
6060

61-
exports.puts = util.deprecate(function() {
61+
exports.puts = util.deprecate(() => {
6262
for (var i = 0, len = arguments.length; i < len; ++i) {
6363
process.stdout.write(arguments[i] + '\n');
6464
}

doc/api/zlib.markdown

+6-2
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,20 @@ the convenience methods.
2929

3030
```js
3131
const input = '.................................';
32-
zlib.deflate(input, function(err, buffer) {
32+
zlib.deflate(input, (err, buffer) => {
3333
if (!err) {
3434
console.log(buffer.toString('base64'));
35+
} else {
36+
// handle error
3537
}
3638
});
3739

3840
const buffer = new Buffer('eJzT0yMAAGTvBe8=', 'base64');
39-
zlib.unzip(buffer, function(err, buffer) {
41+
zlib.unzip(buffer, (err, buffer) => {
4042
if (!err) {
4143
console.log(buffer.toString());
44+
} else {
45+
// handle error
4246
}
4347
});
4448
```

0 commit comments

Comments
 (0)