0
我在写一个nodejs模块并尝试将事件发送器绑定到“scrappy.get”。似乎我只能将它绑定到“scrappy”....所以“scrappy.get(key).on('complete'....”不起作用。将事件发送给子对象
如何将事件发送给子对象 '得到'
我模块的NodeJS?
var util = require('util'),
EventEmitter = require('events').EventEmitter;
function Scrappy(id) {
}
util.inherits(Scrappy, EventEmitter);
Scrappy.prototype.get = function (key) {
var self = this;
self.emit('complete', "here");
**return this;**
}
module.exports = Scrappy;
我的应用程序的NodeJS代码:
var Scrappy = require('./scrappy.js')
var scrappy = new Scrappy("1234");
scrappy.get("name").on('complete', function(data){
console.log("secondary");
});
结果:
scrappy.get("name").on('complete', function(data){
^
TypeError: Cannot call method 'on' of undefined
编辑:解决。添加“返回这个;”为我解决它。谢谢!
嗯,似乎没有工作。在nodejs应用程序中,我仍然得到: scrappy.get(“name”)。on('complete',function(data){ ^ TypeError:无法读取未定义的属性'on' – klinquist 2014-12-05 21:37:37
主要问题是你没有从'get()'返回Scrappy实例能够调用'.on()'。 – mscdex 2014-12-05 21:50:08
mscdex,感谢您的协助,但它不适合我。 – klinquist 2014-12-05 21:52:43