Skip to content

Instantly share code, notes, and snippets.

@sandeep45
Created June 19, 2012 17:25
Show Gist options
  • Save sandeep45/2955409 to your computer and use it in GitHub Desktop.
Save sandeep45/2955409 to your computer and use it in GitHub Desktop.
example of inheriting EventEmitter in node
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");
@sandeep45
Copy link
Author

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment