Created
June 19, 2012 17:25
-
-
Save sandeep45/2955409 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); | |
var charlie = new dog(); | |
console.info(charlie.name); | |
charlie.on("bark", function(){ | |
console.info("in the dog has barked listner"); | |
}); | |
charlie.emit("bark"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
the dog class has properties on the prototype chain. This means all instances of dog can do
instance1.name
and we will get i am a dog.Now when i do inhertence the way node and js community recommends, i loose the original prototype. What am i doing wrong here?