Skip to content Skip to sidebar Skip to footer

Check If Object Is Instance Of Eventemitter

I am a looking for a way, other than duck typing, to discover if an object inherits from EventEmitter https://nodejs.org/api/events.html I suppose I could just check if the object

Solution 1:

To check if an object is an instance of an EventEmmitter you can compare it with the EventEmitter from within node. Just require the "events" module which will expose an EventEmmitter.

I found and modified a little snippet for you:

var http = require("http");

http.get("http://nodejs.org/", function (res) {
    // res is an EventEmitter that represents the HTTP responseconsole.log(res instanceofrequire("events").EventEmitter); // trueconsole.log(typeof res); // object
});

Post a Comment for "Check If Object Is Instance Of Eventemitter"