[英]How to extend object-literal objects for events in NodeJS?
I am trying to have my Node.js object cast events . 我试图让我的Node.js对象投射事件 。 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. 我习惯编写ExtJS语法,所以我更喜欢对象文字中的所有内容。
// 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? 现在我如何允许
myObject
发出事件?
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? 如何在保持对象文字符号的同时允许
myObject
发出事件? So many confusing ways to do it, but not one within those JSON- like notated objects. 这么多令人困惑的方法,但不是那些JSON 类似的标记对象。
Why not use composition 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. 因为
obj
的原型在创建时被修复为Object.prototype
,你不能在以后更改它。
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: 因为你只想要一个单一实例,我认为它有一定道理创建的实例
EventEmitter
和分配属性给它:
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. 你在这里不需要
util.inherits
,因为你只有一个实例,所以没有设置链。
This is a completely wild guess, as I have only coded JavaScript for browsers. 这是一个完全疯狂的猜测,因为我只为浏览器编写了JavaScript。 However, can't you just call
EventEmitter
on your object after creating your object literal? 但是, 在创建对象文字后 ,您不能只在对象上调用
EventEmitter
吗?
myObject = {
myFunction: function() {
console.log('foo');
},
urFunction: function() {
console.log('bar');
}
};
EventEmitter.call(myObject);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.