mirror of https://github.com/nodejs/node.git
Docs for util.inherits
parent
3422263414
commit
5749f91814
|
@ -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…
Reference in New Issue