-
-
Save scott2449/2955827 to your computer and use it in GitHub Desktop.
example of inheriting EventEmitter in node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var emitter = require('events').EventEmitter; | |
var util = require('util'); | |
var dog = function(){}; | |
//dog.prototype = { | |
// name: "i am a dog", | |
//}; | |
// 1st way of inheriting | |
//dog.prototype.__proto__ = new emitter(); | |
// 2nd way in inheritence | |
//dog.prototype = Object.create(emitter.prototype); | |
// 3rd way of inheritence | |
util.inherits(dog, emitter); | |
dog.prototype.name = "i am a dog"; | |
var charlie = new dog(); | |
console.info(charlie.name); | |
charlie.on("bark", function(){ | |
console.info("in the dog has barked listner"); | |
}); | |
charlie.emit("bark"); |
okay, what abt properties of EventEmitter class which are not on the prototype chain. shoulnd;t they also be inherited and available in dog?
i am looking at the eventEmitter code and it doesnt have any properties which it puts on this. so it doesnt matter in this case, but i would still wanna know in general?
imagine if EventEmitter was like this:
var emitter = function(){
this.group = [];
this.name = "blaba";
}
emitter.prototype.emit = function(){}
emitter.prototype.on = function(){}
etc. etc. etc.
with the above class def, classes which inherit from emitter wont have group and name
I ran the example i sent and the charlie was an event emitter and had his
properties
…On Tue, Jun 19, 2012 at 3:43 PM, Sandeep Arneja < ***@***.*** > wrote:
okay, what abt properties of EventEmitter class which are not on the
prototype chain. shoulnd;t they also be inherited and available in dog?
t
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/2955827
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you look at the inherits code you'll see how it wipes all but the constructor of the left arg. Try what I have above defining the prototype methods/properties post inherits