How to extend object literature for events in NodeJS?

I am trying to create a Node.js translation event object. Now this is not a problem if I create β€œstatic” objects and create an instance of them, but how to do this when my object does not have a static grandfather, for example, when the object was created using the object literal symbol?

I use ExtJS to write the syntax, so I prefer everything in the object literal.

// var EventEmitter = require('events').EventEmitter; // How where when? myObject = { myFunction: function() { console.log('foo'); }, urFunction: function() { console.log('bar'); } }; 

This is an object. It has no constructor because no more instances are needed.

Now, how can I let myObject emit events?

I tried and tried to adapt the code, but I can't get it to work without rewriting my object into a form with a constructor like this:

 var EventEmitter = require('events').EventEmitter; var util = require('util'); function myClass() { EventEmitter.call(this); myFunction: function() { this.emit('foo'); }, urFunction: function() { this.emit('bar'); } }; myClass.prototype = Object.create(EventEmitter.prototype); // or // util.inherits(myClass, EventEmitter); var myObject = new myClass; // Shouldn't be necessary for my single-instance case 

Or adding my functions / prototypes to those constructed as follows:

 var EventEmitter = require('events').EventEmitter; var util = require('util'); var myObject = new EventEmitter(); // or // var myObject = Object.create(new EventEmitter); // Dunno difference myObject.myFunction = function() { this.emit('foo'); }, myObject.urFunction = function() { this.emit('bar'); } }; util.inherits(myObject, EventEmitter); 

How to allow myObject emit events while maintaining object literal notation? There are so many confusing ways to do this, but not one of those JSON-like note objects.

+8
javascript events object-literal eventemitter
source share
3 answers

Why not use composition instead of inheritance?

 var myObject = { myFunction: function() { console.log('foo'); }, urFunction: function() { console.log('bar'); }, emitter: new EventEmitter() } 
+9
source share

Unfortunately, your desired object literal syntax is not possible. It's because

 var obj = {}; 

equivalently

 var obj = Object.create(Object.prototype); 

that the obj prototype is bound to Object.prototype when created, and you cannot change it later.

Since you just need one instance, I would say that it makes sense to create an EventEmitter instance and assign properties to it:

 var EventEmitter = require('events').EventEmitter; var obj = new EventEmitter(); obj.doStuff = function() { this.emit('stuff'); }; 

You don't need util.inherits , since you only have one instance, so there is no chain to configure.

+7
source share

This is a completely wild assumption since I only have JavaScript encoded for browsers. However, can't you just call EventEmitter on your object after creating the object literal?

 myObject = { myFunction: function() { console.log('foo'); }, urFunction: function() { console.log('bar'); } }; EventEmitter.call(myObject); 
-one
source share

All Articles