From 9a91ffbbdeca0f82e496c5f6b095c863ca778c8a Mon Sep 17 00:00:00 2001 From: Shahid Shaikh Date: Fri, 19 Aug 2016 21:30:11 +0530 Subject: [PATCH] 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 Reviewed-By: Yorkie Liu Reviewed-By: Robert Jefe Lindstaedt --- doc/api/util.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/doc/api/util.md b/doc/api/util.md index 5b59824b6255fe..d183bfec595d87 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -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.