最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to extend object-literal objects for events in NodeJS? - Stack Overflow

programmeradmin7浏览0评论

I am trying to have my Node.js object cast events. Now this is not a problem if I make 'static' objects and instantiate them, but how do I do this when my object has no static grandpa, like when an object was created using object literal notation?

I am used to writing ExtJS syntax so I prefer everything in 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 does not have a constructor, because there don't need to be more instances.

Now how do I allow myObject to emit events?

I have tried and tried to adapt code, but I cannot get it to work without rewriting my object to a form with a constructor like so:

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 something constructed like so:

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 do I allow myObject to emit events, while keeping the object literal notation? So many confusing ways to do it, but not one within those JSON-like notated objects.

I am trying to have my Node.js object cast events. Now this is not a problem if I make 'static' objects and instantiate them, but how do I do this when my object has no static grandpa, like when an object was created using object literal notation?

I am used to writing ExtJS syntax so I prefer everything in 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 does not have a constructor, because there don't need to be more instances.

Now how do I allow myObject to emit events?

I have tried and tried to adapt code, but I cannot get it to work without rewriting my object to a form with a constructor like so:

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 something constructed like so:

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 do I allow myObject to emit events, while keeping the object literal notation? So many confusing ways to do it, but not one within those JSON-like notated objects.

Share Improve this question edited May 31, 2012 at 16:20 Redsandro asked May 31, 2012 at 16:04 RedsandroRedsandro 11.4k15 gold badges80 silver badges111 bronze badges 2
  • Very good question, I like using Object.create() and literals from browser based environments but using node js for a while now, my conclusion is that ES5 like object handling is somewhat underused. Reason might be that functions like util.inherits do not exist for handling object literals. – vanthome Commented Mar 27, 2013 at 13:31
  • Please see this answer: stackoverflow./questions/24925115/… – victorwoo Commented Jul 25, 2014 at 6:04
Add a ment  | 

3 Answers 3

Reset to default 9

Why not use position instead of inheritance?

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

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

var obj = {};

is equivalent to

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

in that obj's prototype is fixed to Object.prototype at creation and you can't change that later on.

Since you just want a single instance, I'd argue that it does make sense to create an instance of EventEmitter 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 here, as you just have one instance, so there's no chain to setup.

This is a pletely wild guess, as I have only coded JavaScript for browsers. However, can't you just call EventEmitter on your object after creating your object literal?

myObject = {
    myFunction: function() {
        console.log('foo');
    },
    urFunction: function() {
        console.log('bar');
    }
};
EventEmitter.call(myObject);
发布评论

评论列表(0)

  1. 暂无评论