The problem with __proto__ is not that you use prototypes instead of constructors. The problem is that this is the wrong way to use prototypes. But you do not need a prototype. You want to mix. Using __proto__ is a hack that avoids the work of creating mixin. If you want to mix, you must do it manually, without prototypes.
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");
source share