Step 1: Bring in the modules
// events contains the EventEmitter // class we will inherit from var events = require('events'); // and util contains `inherit` which // we will use to set up our object's inheritance var util = require('util');
Step 2: Construct your class, followed by the EventEmitter's
You will want to call the EventEmitter's constructor at the end of your own. It's standard practice when inheriting from another class that you call into the parent's constructor in order to preserve any functionality within it. I've excluded it before, and it's not the end of the world, it just sets some default properties. You can see what the constructor in Node does on Github.
var MyClass = function () { // Your class' constructor magic // Call the EventEmitter constructor events.EventEmitter.call(this); }
Step 3: Set up the prototypal inheritance
util.inherits(MyClass, events.EventEmitter);Well that was easy. What this does is set up the prototype chain for us. You can read more about the util.inherits again on Node's website.
The Final Code
var util = require("util"); var events = require("events"); function MyClass() { // Your class' constructor magic events.EventEmitter.call(this); } util.inherits(MyClass, events.EventEmitter); MyClass.prototype.emitTest = function(data) { this.emit("data", data); } // Consumption var instance = new MyClass(); instance.on('data', function (msg) { console.log(msg); }); instance.emitTest("Hello World"); //=> Hello World
8 comments:
It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
python training institute in marathahalli
python training institute in btm
Python training course in Chennai
Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.
Data Science course in Chennai
Data science course in bangalore
Data science course in pune
Data science online course
This is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.I like it and help me to development very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge.
Best Devops Training in pune
Microsoft azure training in Bangalore
Power bi training in Chennai
Awesome Blog. The Content is Really neat and the Concept was explained Very Clear.Keep Updating this Article.
Data Science Training Course In Chennai | Data Science Training Course In Anna Nagar | Data Science Training Course In OMR | Data Science Training Course In Porur | Data Science Training Course In Tambaram | Data Science Training Course In Velachery
Wow it is really super and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.
Java training in Chennai
Java training in Bangalore
Java training in Hyderabad
Java Training in Coimbatore
Java Online Training
Nice . I really enjoyed very much with this article here. Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article.thank you for sharing such a great blog with us. expecting for your.
Data Science Training In Chennai
Data Science Online Training In Chennai
Data Science Training In Bangalore
Data Science Training In Hyderabad
Data Science Training In Coimbatore
Data Science Training
Data Science Online Training
Thanks for posting a very useful blog. Keep us posted!
data science training in chennai
ccna training in chennai
iot training in chennai
cyber security training in chennai
ethical hacking training in chennai
This post is very simple to read and appreciate without leaving any details out. Great work!
data science training in hyderabad
Post a Comment