diff options
author | Marco Rogers <marco.rogers@gmail.com> | 2010-11-21 08:28:19 +0300 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2010-11-22 06:03:23 +0300 |
commit | 5749f9181480dc69c8e4b1d4755d50b8d9af56c7 (patch) | |
tree | ac39eb5514373b634379eea2b40e93e83119612e /doc | |
parent | 342226341467151f753814b1c1b4fbb0d8aac5d3 (diff) |
Docs for util.inherits
Diffstat (limited to 'doc')
-rw-r--r-- | doc/api/util.markdown | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/doc/api/util.markdown b/doc/api/util.markdown index 1c077f6ef2b..6076239658d 100644 --- a/doc/api/util.markdown +++ b/doc/api/util.markdown @@ -48,3 +48,37 @@ When `writeableStream.write(data)` returns `false` `readableStream` will be paused until the `drain` event occurs on the `writableStream`. `callback` gets an error as its only argument and is called when `writableStream` is closed or when an error occurs. + + +### util.inherits(constructor, superConstructor) + +Inherit the prototype methods from one +[constructor](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/constructor) +into another. The prototype of `constructor` will be set to a new +object created from `superConstructor`. + +As an additional convenience, `superConstructor` will be accessible +through the `constructor.super_` property. + + var util = require("util"); + var events = require("events"); + + function MyStream() { + events.EventEmitter.call(this); + } + + util.inherits(MyStream, events.EventEmitter); + + MyStream.prototype.write = function(data) { + this.emit("data", data); + } + + var stream = new MyStream(); + + console.log(stream instanceof events.EventEmitter); // true + console.log(MyStream.super_ === events.EventEmitter); // true + + stream.on("data", function(data) { + console.log('Received data: "' + data + '"'); + }) + stream.write("It works!"); // Received data: "It works!" |