使用__proto__的Mixin EventEmitter方法

时间:2012-09-26 08:35:51

标签: node.js events eventemitter

给定一个任意对象,我想让它成为一个EventEmitter:

var obj = {}
// mixin eventemitter
obj.on('event', ...)
obj.emit('event')

另外,当我输入obj时,我不希望它将EventEmitter方法显示为方法。来自CLI:

> obj
{}

因此,我现在正在做:

function mixinEventEmitter(obj) {
  obj.__proto__ = EventEmitter.prototype
  return obj
}

但有人说使用__proto__是一种反模式:Node.js - inheriting from EventEmitter

我做得对吗?你有更好的方法吗?感谢。

2 个答案:

答案 0 :(得分:2)

通常的方法是使用util.inherits(链接的文档包含一个几乎完全符合您要求的示例)。

答案 1 :(得分:1)

__proto__的问题不在于您使用原型而不是构造函数。问题是使用原型是错误的方法。但是你不想要原型。你想要一个混合。使用__proto__是一个黑客,可以避免做创建mixin的工作。如果你想要一个mixin,你必须手动完成,没有原型。

var EventEmitter = require("events").EventEmitter,
    obj = {};

function emitter(obj) {
    // copy EventEmitter prototype to obj, but make properties
    // non-enumerable
    for (var prop in EventEmitter.prototype) {
        Object.defineProperty(obj, prop, {
            configurable: true,
            writable: true,
            value: EventEmitter.prototype[prop]
        });
    }

    // also, make sure the following properties are hidden
    // before the constructor adds them
    ["domain", "_events", "_maxListeners"].forEach(function(prop) {
        Object.defineProperty(obj, prop, {
            configurable: true,
            writable: true,
            value: undefined
        });
    });

    // call EventEmitter constructor on obj
    EventEmitter.call(obj);

    // return the obj, which should now function as EventEmitter
    return obj;
}

emitter(obj);
obj.on("event", console.log.bind(console));
obj.emit("event", "foo");