我有一个特定于javascript而不是node.js的继承的问题:
// foo.js
var events = require('events');
var util = require('util');
var Foo = (function(){
var foo = {};
events.EventEmitter.call(foo);
foo.init = function(){
foo.emit('bar');
};
util.inherits(parser, events.EventEmitter);
return {
bind: foo.init,
on: foo.on // I suppose this resolves to event emitter's on method
};
}());
module.exports = Foo;
然后,当我简单地尝试调用时(比如在 main.js 中):
var foo = require('./foo.js');
foo.on('bar', function(){ ... });
在我打电话之前就产生了一个错误foo.init();
:
foo.on('bar', function(){ ... })
^
TypeError: Property 'on' of object #<Object> is not a function
当我选择以 IIFE 模式编码时,是否可以从 EventEmitter 继承?还是我的错误与其他有关?