Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

EventEmitter (yeah...) Prototypal Events Issue #7103

@skeggse

Description

@skeggse

This is a "simple" issue, but the need for the event emitter to remain frozen may simply mean it's a tolerable issue.

var Emitter = require('events').EventEmitter;
var emitter = new Emitter();

emitter.emit('valueOf'); // => true

// bad example
Object.defineProperty(Object.prototype, 'tosser', {
  enumerable: false,
  writable: false,
  configurable: false,
  value: function() {
    throw new Error("this wasn't supposed to happen");
  }
});

// not necessarily the expected outcome
emitter.emit('tosser'); // Error

// betterish example
Object.defineProperty(Object.prototype, 'badDay', {
  enumerable: false,
  writable: false,
  configurable: false,
  value: function() {
    // basically, has side effects
    send('money').to('group').where('max(group.hate)');
    // perhaps a moot point because no matter what object prototype methods shouldn't have side effects...
  }
});

// doesn't do what's expected
emitter.emit('badDay'); // => true

In summary, because the event emitter does not handle prototypal methods on the _events object, there could be conflict, and it certainly makes the emit's return method return true where it otherwise shouldn't.

Best-case change given that the API is frozen: set the _events object to Object.create(null). This would, incidentally, slightly reduce the time taken to emit an event because there would be no prototype chain.

Luckily, because _events is a private property, this shouldn't be a breaking change. Even if someone were tampering with the _events object, they should have the knowledge to not depend on its methods.

Thoughts?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions