Skip to content

Instantly share code, notes, and snippets.

@Trott
Created December 4, 2015 03:35
Show Gist options
  • Save Trott/a865746d08814af087b7 to your computer and use it in GitHub Desktop.
Save Trott/a865746d08814af087b7 to your computer and use it in GitHub Desktop.
Can Node use Symbols for the name of an Event? I'd say yep...
'use strict';
const util = require('util');
const EventEmitter = require('events');
function MyEventEmitter() {
EventEmitter.call(this);
}
// Inherit functions from `EventEmitter`'s prototype
util.inherits(MyEventEmitter, EventEmitter);
const foo = new MyEventEmitter();
const symbol = Symbol('Hi, Bengie! Err... I mean, Bryan!');
foo.on(symbol, function () { console.log('symbol event fired!'); })
foo.emit(symbol);
@kessler
Copy link

kessler commented May 3, 2018

3 years later...

let event = Symbol('event')
let EventEmitter = require('events')
let x = new EventEmitter()
x.on(event, console.log)

console.log('emit with symbol:')
x.emit(event, 1)

console.log('emit with string:')
x.emit('event', 1)

console.log('emit with symbol.toString:')
x.emit(event.toString(), 1)

The output will be:

emit with symbol:
1
emit with string:
emit with symbol.toString:

So maybe symbols are not coerced into strings after all / anymore?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment