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>
13 files changed:
This "Hello world" example is a simple Addon, written in C++, that is the
equivalent of the following JavaScript code:
This "Hello world" example is a simple Addon, written in C++, that is the
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`:
// test.js
const addon = require('./build/Release/addon');
// test.js
const addon = require('./build/Release/addon');
console.log(msg); // 'hello world'
});
```
console.log(msg); // 'hello world'
});
```
```js
assert.doesNotThrow(
```js
assert.doesNotThrow(
throw new TypeError('Wrong value');
},
SyntaxError
throw new TypeError('Wrong value');
},
SyntaxError
```js
assert.doesNotThrow(
```js
assert.doesNotThrow(
throw new TypeError('Wrong value');
},
TypeError
throw new TypeError('Wrong value');
},
TypeError
```js
assert.doesNotThrow(
```js
assert.doesNotThrow(
throw new TypeError('Wrong value');
},
TypeError,
throw new TypeError('Wrong value');
},
TypeError,
throw new Error('Wrong value');
},
Error
throw new Error('Wrong value');
},
Error
throw new Error('Wrong value');
},
/value/
throw new Error('Wrong value');
},
/value/
throw new Error('Wrong value');
},
function(err) {
throw new Error('Wrong value');
},
function(err) {
connecting... ok
break in /home/indutny/Code/git/indutny/myscript.js:1
1 x = 5;
connecting... ok
break in /home/indutny/Code/git/indutny/myscript.js:1
1 x = 5;
- 2 setTimeout(function () {
```js
// myscript.js
x = 5;
```js
// myscript.js
x = 5;
-setTimeout(function () {
debugger;
console.log('world');
}, 1000);
debugger;
console.log('world');
}, 1000);
connecting... ok
break in /home/indutny/Code/git/indutny/myscript.js:1
1 x = 5;
connecting... ok
break in /home/indutny/Code/git/indutny/myscript.js:1
1 x = 5;
- 2 setTimeout(function () {
3 debugger;
debug> cont
< hello
break in /home/indutny/Code/git/indutny/myscript.js:3
1 x = 5;
3 debugger;
debug> cont
< hello
break in /home/indutny/Code/git/indutny/myscript.js:3
1 x = 5;
- 2 setTimeout(function () {
3 debugger;
4 console.log('world');
5 }, 1000);
debug> next
break in /home/indutny/Code/git/indutny/myscript.js:4
3 debugger;
4 console.log('world');
5 }, 1000);
debug> next
break in /home/indutny/Code/git/indutny/myscript.js:4
- 2 setTimeout(function () {
3 debugger;
4 console.log('world');
5 }, 1000);
3 debugger;
4 console.log('world');
5 }, 1000);
debug> c
break in test/fixtures/break-in-module/mod.js:23
21
debug> c
break in test/fixtures/break-in-module/mod.js:23
21
- 22 exports.hello = function() {
+ 22 exports.hello = () => {
23 return 'hello from module';
24 };
25
23 return 'hello from module';
24 };
25
const d = domain.create();
function readSomeFile(filename, cb) {
const d = domain.create();
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
return cb(er, data ? JSON.parse(data) : null);
}));
// if this throws, it will also be passed to the domain
return cb(er, data ? JSON.parse(data) : null);
}));
const d = domain.create();
function readSomeFile(filename, cb) {
const d = domain.create();
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
// callback since it is assumed to be the 'Error' argument
// and thus intercepted by the domain.
// note, the first argument is never passed to the
// callback since it is assumed to be the 'Error' argument
// and thus intercepted by the domain.
util.inherits(MyEmitter, EventEmitter);
const myEmitter = new MyEmitter();
util.inherits(MyEmitter, EventEmitter);
const myEmitter = new MyEmitter();
-myEmitter.on('event', function() {
+myEmitter.on('event', () => {
console.log('an event occurred!');
});
myEmitter.emit('event');
console.log('an event occurred!');
});
myEmitter.emit('event');
class MyEmitter extends EventEmitter {}
const myEmitter = new MyEmitter();
class MyEmitter extends EventEmitter {}
const myEmitter = new MyEmitter();
-myEmitter.on('event', function() {
+myEmitter.on('event', () => {
console.log('an event occurred!');
});
myEmitter.emit('event');
console.log('an event occurred!');
});
myEmitter.emit('event');
-var callback = function(stream) {
+var callback = (stream) => {
console.log('someone connected!');
};
server.on('connection', callback);
console.log('someone connected!');
};
server.on('connection', callback);
`/etc/passwd` can be read and written by the current process.
```js
`/etc/passwd` can be read and written by the current process.
```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');
});
```
```js
const PI = Math.PI;
```js
const PI = Math.PI;
-exports.area = function (r) {
- return PI * r * r;
-};
+exports.area = (r) => 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
```js
// assigning to exports will not modify module, must use module.exports
```js
// assigning to exports will not modify module, must use module.exports
-module.exports = function(width) {
+module.exports = (width) => {
- area: function() {
- return width * width;
- }
+ area: () => width * width
```js
function require(...) {
// ...
```js
function require(...) {
// ...
- function (module, exports) {
+ ((module, exports) => {
// Your module code here
exports = some_func; // re-assigns exports, exports is no longer
// a shortcut, and nothing is exported.
module.exports = some_func; // makes your module export 0
// Your module code here
exports = some_func; // re-assigns exports, exports is no longer
// a shortcut, and nothing is exported.
module.exports = some_func; // makes your module export 0
- } (module, module.exports);
+ })(module, module.exports);
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
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
-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
emitted, or you can use the [`'rejectionHandled'`][] event.
`resource.loaded`, preventing the `'unhandledRejection'` event from being
emitted, or you can use the [`'rejectionHandled'`][] event.
This API is hazardous. If you do this:
```js
This API is hazardous. If you do this:
```js
-maybeSync(true, function() {
For example, a `console.log` equivalent could look like this:
```js
For example, a `console.log` equivalent could look like this:
```js
-console.log = function(msg) {
+console.log = (msg) => {
process.stdout.write(`${msg}\n`);
};
```
process.stdout.write(`${msg}\n`);
};
```
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);
});
```
Type '.help' for options.
> a = [ 1, 2, 3];
[ 1, 2, 3 ]
Type '.help' for options.
> a = [ 1, 2, 3];
[ 1, 2, 3 ]
-> a.forEach(function (v){
... console.log(v);
... });
1
... console.log(v);
... });
1
```
> var obj = {foo: 'this will not show up in the inspect() output'};
undefined
```
> var obj = {foo: 'this will not show up in the inspect() output'};
undefined
-> obj.inspect = function() {
... return {bar: 'baz'};
... };
[Function]
... return {bar: 'baz'};
... };
[Function]
Readable.call(this, options);
this._source = getLowlevelSourceObject();
Readable.call(this, options);
this._source = getLowlevelSourceObject();
// 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))
- self._source.readStop();
+ if (!this.push(chunk))
+ 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() {
- self.push(null);
+ this._source.onend = () => {
+ this.push(null);
```js
const util = require('util');
```js
const util = require('util');
-exports.puts = util.deprecate(function() {
+exports.puts = util.deprecate(() => {
for (var i = 0, len = arguments.length; i < len; ++i) {
process.stdout.write(arguments[i] + '\n');
}
for (var i = 0, len = arguments.length; i < len; ++i) {
process.stdout.write(arguments[i] + '\n');
}
```js
const input = '.................................';
```js
const input = '.................................';
-zlib.deflate(input, function(err, buffer) {
+zlib.deflate(input, (err, buffer) => {
if (!err) {
console.log(buffer.toString('base64'));
if (!err) {
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) {
console.log(buffer.toString());
if (!err) {
console.log(buffer.toString());
+ } else {
+ // handle error