import EventEmitter from 'events';
const emitter = new EventEmitter();
const spread = { ...emitter };
console.log(spread.on); // undefined
console.log(emitter.on); // [Function: addListener]
Why does spreading drop this method? And how can I stop that from happening?
Why does spreading drop this method?
Because spread syntax only copies own, enumerable properties from the target. It doesn't copy inherited properties, and it doesn't copy the prototype of the object.
I'm not sure that you can clone an EventEmitter, but if you can it's not going to be a simple one-liner. You'd have to create a new instance, get the events it has listeners for (eventNames), then get the listeners for each event (listeners), etc. And even then, some things are subclasses of EventEmitter...