Browse Source

Docs for util.inherits

v0.7.4-release
Marco Rogers 14 years ago
committed by Ryan Dahl
parent
commit
5749f91814
  1. 34
      doc/api/util.markdown

34
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!"

Loading…
Cancel
Save