doc: add es6 code example in util.md
Added class/extends example to util.md PR-URL: https://github.com/nodejs/node/pull/8183 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Yorkie Liu <yorkiefixer@gmail.com> Reviewed-By: Robert Jefe Lindstaedt <robert.lindstaedt@gmail.com>
This commit is contained in:
parent
ce8753a4ce
commit
9a91ffbbde
@ -165,6 +165,30 @@ stream.on('data', (data) => {
|
||||
stream.write('It works!'); // Received data: "It works!"
|
||||
```
|
||||
|
||||
ES6 example using `class` and `extends`
|
||||
|
||||
```js
|
||||
const util = require('util');
|
||||
const EventEmitter = require('events');
|
||||
|
||||
class MyStream extends EventEmitter {
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
write(data) {
|
||||
this.emit('data', data);
|
||||
}
|
||||
}
|
||||
|
||||
const stream = new MyStream();
|
||||
|
||||
stream.on('data', (data) => {
|
||||
console.log(`Received data: "${data}"`);
|
||||
});
|
||||
stream.write('With ES6');
|
||||
|
||||
```
|
||||
|
||||
## util.inspect(object[, options])
|
||||
|
||||
* `object` {any} Any JavaScript primitive or Object.
|
||||
|
Loading…
x
Reference in New Issue
Block a user