我一直看到这个:
Parrot.prototype.__proto__ = EventEmitter.prototype;
有了这个,每次你 build 一只新鹦鹉时,它都会发出叫声。
但是,假设我构造一个具有函数的对象并且不打算创建多个实例:
var parrot = {
squawk: function(whatYouSaid){
this.emit("SQUAWK!!!!", whatYouSaid);
}
}
我如何扩展EventEmitter
?我尝试了这个,但没有成功:
_.extend(parrot, (new EventEmitter()));
最佳答案
您应该将 EventEmitter.prototype
扩展/分配给您的对象:
_.assign(parrot, EventEmitter.prototype);
关于node.js - Node : extend EventEmitter to non-prototype object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31252690/