diff --git a/lib/events.js b/lib/events.js index bd6a36866187e5..79a722674c3eea 100644 --- a/lib/events.js +++ b/lib/events.js @@ -162,23 +162,22 @@ function emitMany(handler, isFn, self, args) { } } -EventEmitter.prototype.emit = function emit(type) { - var er, handler, len, args, i, events, domain; - var needDomainExit = false; - var doError = (type === 'error'); +EventEmitter.prototype.emit = function emit(type, ...args) { + let doError = (type === 'error'); - events = this._events; + const events = this._events; if (events !== undefined) doError = (doError && events.error === undefined); else if (!doError) return false; - domain = this.domain; + const domain = this.domain; // If there is no 'error' event listener then throw. if (doError) { - if (arguments.length > 1) - er = arguments[1]; + let er; + if (args.length > 0) + er = args[0]; if (domain !== null && domain !== undefined) { if (!er) { const errors = lazyErrors(); @@ -202,37 +201,32 @@ EventEmitter.prototype.emit = function emit(type) { return false; } - handler = events[type]; + const handler = events[type]; if (handler === undefined) return false; + let needDomainExit = false; if (domain !== null && domain !== undefined && this !== process) { domain.enter(); needDomainExit = true; } - var isFn = typeof handler === 'function'; - len = arguments.length; - switch (len) { - // fast cases - case 1: + const isFn = typeof handler === 'function'; + switch (args.length) { + case 0: emitNone(handler, isFn, this); break; + case 1: + emitOne(handler, isFn, this, args[0]); + break; case 2: - emitOne(handler, isFn, this, arguments[1]); + emitTwo(handler, isFn, this, args[0], args[1]); break; case 3: - emitTwo(handler, isFn, this, arguments[1], arguments[2]); - break; - case 4: - emitThree(handler, isFn, this, arguments[1], arguments[2], arguments[3]); + emitThree(handler, isFn, this, args[0], args[1], args[2]); break; - // slower default: - args = new Array(len - 1); - for (i = 1; i < len; i++) - args[i - 1] = arguments[i]; emitMany(handler, isFn, this, args); }