Browse Source

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: https://github.com/nodejs/node/pull/4832
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
v5.x
Benjamin Gruenbaum 9 years ago
committed by Rod Vagg
parent
commit
e65d3638c0
  1. 6
      doc/api/addons.markdown
  2. 12
      doc/api/assert.markdown
  3. 12
      doc/api/debugger.markdown
  4. 4
      doc/api/domain.markdown
  5. 6
      doc/api/events.markdown
  6. 2
      doc/api/fs.markdown
  7. 19
      doc/api/modules.markdown
  8. 6
      doc/api/process.markdown
  9. 2
      doc/api/readline.markdown
  10. 4
      doc/api/repl.markdown
  11. 11
      doc/api/stream.markdown
  12. 2
      doc/api/util.markdown
  13. 8
      doc/api/zlib.markdown

6
doc/api/addons.markdown

@ -44,7 +44,9 @@ be used as a starting-point for your own Addon.
This "Hello world" example is a simple Addon, written in C++, that is the This "Hello world" example is a simple Addon, written in C++, that is the
equivalent of the following JavaScript code: equivalent of the following JavaScript code:
module.exports.hello = function() { return 'world'; }; ```js
module.exports.hello = () => 'world';
```
First, create the file `hello.cc`: First, create the file `hello.cc`:
@ -368,7 +370,7 @@ To test it, run the following JavaScript:
// test.js // test.js
const addon = require('./build/Release/addon'); const addon = require('./build/Release/addon');
addon(function(msg){ addon((msg) => {
console.log(msg); // 'hello world' console.log(msg); // 'hello world'
}); });
``` ```

12
doc/api/assert.markdown

@ -139,7 +139,7 @@ matching error type in the assertion:
```js ```js
assert.doesNotThrow( assert.doesNotThrow(
function() { () => {
throw new TypeError('Wrong value'); throw new TypeError('Wrong value');
}, },
SyntaxError SyntaxError
@ -151,7 +151,7 @@ However, the following will result in an `AssertionError` with the message
```js ```js
assert.doesNotThrow( assert.doesNotThrow(
function() { () => {
throw new TypeError('Wrong value'); throw new TypeError('Wrong value');
}, },
TypeError TypeError
@ -164,7 +164,7 @@ message:
```js ```js
assert.doesNotThrow( assert.doesNotThrow(
function() { () => {
throw new TypeError('Wrong value'); throw new TypeError('Wrong value');
}, },
TypeError, TypeError,
@ -359,7 +359,7 @@ Validate instanceof using constructor:
```js ```js
assert.throws( assert.throws(
function() { () => {
throw new Error('Wrong value'); throw new Error('Wrong value');
}, },
Error Error
@ -370,7 +370,7 @@ Validate error message using [`RegExp`][]:
```js ```js
assert.throws( assert.throws(
function() { () => {
throw new Error('Wrong value'); throw new Error('Wrong value');
}, },
/value/ /value/
@ -381,7 +381,7 @@ Custom error validation:
```js ```js
assert.throws( assert.throws(
function() { () => {
throw new Error('Wrong value'); throw new Error('Wrong value');
}, },
function(err) { function(err) {

12
doc/api/debugger.markdown

@ -15,7 +15,7 @@ $ node debug myscript.js
connecting... ok connecting... ok
break in /home/indutny/Code/git/indutny/myscript.js:1 break in /home/indutny/Code/git/indutny/myscript.js:1
1 x = 5; 1 x = 5;
2 setTimeout(function () { 2 setTimeout(() => {
3 debugger; 3 debugger;
debug> debug>
``` ```
@ -31,7 +31,7 @@ For example, suppose `myscript.js` is written as:
```js ```js
// myscript.js // myscript.js
x = 5; x = 5;
setTimeout(function () { setTimeout(() => {
debugger; debugger;
console.log('world'); console.log('world');
}, 1000); }, 1000);
@ -46,19 +46,19 @@ $ node debug myscript.js
connecting... ok connecting... ok
break in /home/indutny/Code/git/indutny/myscript.js:1 break in /home/indutny/Code/git/indutny/myscript.js:1
1 x = 5; 1 x = 5;
2 setTimeout(function () { 2 setTimeout(() => {
3 debugger; 3 debugger;
debug> cont debug> cont
< hello < hello
break in /home/indutny/Code/git/indutny/myscript.js:3 break in /home/indutny/Code/git/indutny/myscript.js:3
1 x = 5; 1 x = 5;
2 setTimeout(function () { 2 setTimeout(() => {
3 debugger; 3 debugger;
4 console.log('world'); 4 console.log('world');
5 }, 1000); 5 }, 1000);
debug> next debug> next
break in /home/indutny/Code/git/indutny/myscript.js:4 break in /home/indutny/Code/git/indutny/myscript.js:4
2 setTimeout(function () { 2 setTimeout(() => {
3 debugger; 3 debugger;
4 console.log('world'); 4 console.log('world');
5 }, 1000); 5 }, 1000);
@ -135,7 +135,7 @@ Warning: script 'mod.js' was not loaded yet.
debug> c debug> c
break in test/fixtures/break-in-module/mod.js:23 break in test/fixtures/break-in-module/mod.js:23
21 21
22 exports.hello = function() { 22 exports.hello = () => {
23 return 'hello from module'; 23 return 'hello from module';
24 }; 24 };
25 25

4
doc/api/domain.markdown

@ -349,7 +349,7 @@ thrown will be routed to the domain's `'error'` event.
const d = domain.create(); const d = domain.create();
function readSomeFile(filename, cb) { function readSomeFile(filename, cb) {
fs.readFile(filename, 'utf8', d.bind(function(er, data) { fs.readFile(filename, 'utf8', d.bind((er, data) => {
// if this throws, it will also be passed to the domain // if this throws, it will also be passed to the domain
return cb(er, data ? JSON.parse(data) : null); return cb(er, data ? JSON.parse(data) : null);
})); }));
@ -380,7 +380,7 @@ with a single error handler in a single place.
const d = domain.create(); const d = domain.create();
function readSomeFile(filename, cb) { function readSomeFile(filename, cb) {
fs.readFile(filename, 'utf8', d.intercept(function(data) { fs.readFile(filename, 'utf8', d.intercept((data) => {
// note, the first argument is never passed to the // note, the first argument is never passed to the
// callback since it is assumed to be the 'Error' argument // callback since it is assumed to be the 'Error' argument
// and thus intercepted by the domain. // and thus intercepted by the domain.

6
doc/api/events.markdown

@ -37,7 +37,7 @@ function MyEmitter() {
util.inherits(MyEmitter, EventEmitter); util.inherits(MyEmitter, EventEmitter);
const myEmitter = new MyEmitter(); const myEmitter = new MyEmitter();
myEmitter.on('event', function() { myEmitter.on('event', () => {
console.log('an event occurred!'); console.log('an event occurred!');
}); });
myEmitter.emit('event'); myEmitter.emit('event');
@ -54,7 +54,7 @@ const EventEmitter = require('events');
class MyEmitter extends EventEmitter {} class MyEmitter extends EventEmitter {}
const myEmitter = new MyEmitter(); const myEmitter = new MyEmitter();
myEmitter.on('event', function() { myEmitter.on('event', () => {
console.log('an event occurred!'); console.log('an event occurred!');
}); });
myEmitter.emit('event'); myEmitter.emit('event');
@ -364,7 +364,7 @@ Removes the specified `listener` from the listener array for the specified
`event`. `event`.
```js ```js
var callback = function(stream) { var callback = (stream) => {
console.log('someone connected!'); console.log('someone connected!');
}; };
server.on('connection', callback); server.on('connection', callback);

2
doc/api/fs.markdown

@ -239,7 +239,7 @@ argument will be populated. The following example checks if the file
`/etc/passwd` can be read and written by the current process. `/etc/passwd` can be read and written by the current process.
```js ```js
fs.access('/etc/passwd', fs.R_OK | fs.W_OK, function (err) { fs.access('/etc/passwd', fs.R_OK | fs.W_OK, (err) => {
console.log(err ? 'no access!' : 'can read/write'); console.log(err ? 'no access!' : 'can read/write');
}); });
``` ```

19
doc/api/modules.markdown

@ -20,13 +20,10 @@ The contents of `circle.js`:
```js ```js
const PI = Math.PI; const PI = Math.PI;
exports.area = function (r) { exports.area = (r) => PI * r * r;
return PI * r * r;
}; exports.circumference = (r) => 2 * PI * r;
exports.circumference = function (r) {
return 2 * PI * r;
};
``` ```
The module `circle.js` has exported the functions `area()` and The module `circle.js` has exported the functions `area()` and
@ -53,11 +50,9 @@ The `square` module is defined in `square.js`:
```js ```js
// assigning to exports will not modify module, must use module.exports // assigning to exports will not modify module, must use module.exports
module.exports = function(width) { module.exports = (width) => {
return { return {
area: function() { area: () => width * width
return width * width;
}
}; };
} }
``` ```
@ -498,12 +493,12 @@ To illustrate the behavior, imagine this hypothetical implementation of
```js ```js
function require(...) { function require(...) {
// ... // ...
function (module, exports) { ((module, exports) => {
// Your module code here // Your module code here
exports = some_func; // re-assigns exports, exports is no longer exports = some_func; // re-assigns exports, exports is no longer
// a shortcut, and nothing is exported. // a shortcut, and nothing is exported.
module.exports = some_func; // makes your module export 0 module.exports = some_func; // makes your module export 0
} (module, module.exports); })(module, module.exports);
return module; return module;
} }
``` ```

6
doc/api/process.markdown

@ -175,7 +175,7 @@ var resource = new SomeResource();
In cases like this, you may not want to track the rejection as a developer In cases like this, you may not want to track the rejection as a developer
error like you would for other `'unhandledRejection'` events. To address error like you would for other `'unhandledRejection'` events. To address
this, you can either attach a dummy `.catch(function() { })` handler to this, you can either attach a dummy `.catch(() => { })` handler to
`resource.loaded`, preventing the `'unhandledRejection'` event from being `resource.loaded`, preventing the `'unhandledRejection'` event from being
emitted, or you can use the [`'rejectionHandled'`][] event. emitted, or you can use the [`'rejectionHandled'`][] event.
@ -720,7 +720,7 @@ function maybeSync(arg, cb) {
This API is hazardous. If you do this: This API is hazardous. If you do this:
```js ```js
maybeSync(true, function() { maybeSync(true, () => {
foo(); foo();
}); });
bar(); bar();
@ -954,7 +954,7 @@ A `Writable Stream` to `stdout` (on fd `1`).
For example, a `console.log` equivalent could look like this: For example, a `console.log` equivalent could look like this:
```js ```js
console.log = function(msg) { console.log = (msg) => {
process.stdout.write(`${msg}\n`); process.stdout.write(`${msg}\n`);
}; };
``` ```

2
doc/api/readline.markdown

@ -265,7 +265,7 @@ const rl = readline.createInterface({
input: fs.createReadStream('sample.txt') input: fs.createReadStream('sample.txt')
}); });
rl.on('line', function (line) { rl.on('line', (line) => {
console.log('Line from file:', line); console.log('Line from file:', line);
}); });
``` ```

4
doc/api/repl.markdown

@ -15,7 +15,7 @@ $ node
Type '.help' for options. Type '.help' for options.
> a = [ 1, 2, 3]; > a = [ 1, 2, 3];
[ 1, 2, 3 ] [ 1, 2, 3 ]
> a.forEach(function (v){ > a.forEach((v) => {
... console.log(v); ... console.log(v);
... }); ... });
1 1
@ -139,7 +139,7 @@ For example, if you have defined an `inspect()` function on an object, like this
``` ```
> var obj = {foo: 'this will not show up in the inspect() output'}; > var obj = {foo: 'this will not show up in the inspect() output'};
undefined undefined
> obj.inspect = function() { > obj.inspect = () => {
... return {bar: 'baz'}; ... return {bar: 'baz'};
... }; ... };
[Function] [Function]

11
doc/api/stream.markdown

@ -925,18 +925,17 @@ function SourceWrapper(options) {
Readable.call(this, options); Readable.call(this, options);
this._source = getLowlevelSourceObject(); this._source = getLowlevelSourceObject();
var self = this;
// Every time there's data, we push it into the internal buffer. // Every time there's data, we push it into the internal buffer.
this._source.ondata = function(chunk) { this._source.ondata = (chunk) => {
// if push() returns false, then we need to stop reading from source // if push() returns false, then we need to stop reading from source
if (!self.push(chunk)) if (!this.push(chunk))
self._source.readStop(); this._source.readStop();
}; };
// When the source ends, we push the EOF-signaling `null` chunk // When the source ends, we push the EOF-signaling `null` chunk
this._source.onend = function() { this._source.onend = () => {
self.push(null); this.push(null);
}; };
} }

2
doc/api/util.markdown

@ -58,7 +58,7 @@ Marks that a method should not be used any more.
```js ```js
const util = require('util'); const util = require('util');
exports.puts = util.deprecate(function() { exports.puts = util.deprecate(() => {
for (var i = 0, len = arguments.length; i < len; ++i) { for (var i = 0, len = arguments.length; i < len; ++i) {
process.stdout.write(arguments[i] + '\n'); process.stdout.write(arguments[i] + '\n');
} }

8
doc/api/zlib.markdown

@ -29,16 +29,20 @@ the convenience methods.
```js ```js
const input = '.................................'; const input = '.................................';
zlib.deflate(input, function(err, buffer) { zlib.deflate(input, (err, buffer) => {
if (!err) { if (!err) {
console.log(buffer.toString('base64')); console.log(buffer.toString('base64'));
} else {
// handle error
} }
}); });
const buffer = new Buffer('eJzT0yMAAGTvBe8=', 'base64'); const buffer = new Buffer('eJzT0yMAAGTvBe8=', 'base64');
zlib.unzip(buffer, function(err, buffer) { zlib.unzip(buffer, (err, buffer) => {
if (!err) { if (!err) {
console.log(buffer.toString()); console.log(buffer.toString());
} else {
// handle error
} }
}); });
``` ```

Loading…
Cancel
Save