From c257a1c1d63197f5349c12962558eccc7837a0f5 Mon Sep 17 00:00:00 2001 From: Juuso Lapinlampi Date: Wed, 18 Nov 2015 16:24:14 +0000 Subject: Refactor cheesesteak.js EventEmitter emit function Previous commit c950e85 came with a workaround to resolve a TypeError in EventEmitter.prototype.emit for-loop. That workaround sacrificed some readability for bug fixing due to technical debt. bursey came up with a better solution on IRC which seems to work and improves readability. Something needs to emit an event that wasn't bound with "on", thus the event was not in the _events object. Airbnb ES5 style guide requires to have all variables at top, so we leave the len variable undefined at the top to be defined later. After the _events check, we define len to avoid the TypeError issue while conforming to Airbnb ES5 style guide and improving readability. This commit also adds comments to document the function. Suggested-by: bursey (#/g/partyvan @ Partyvan IRC) --- js/cheesesteak.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/js/cheesesteak.js b/js/cheesesteak.js index 90802d3..31a8fd9 100644 --- a/js/cheesesteak.js +++ b/js/cheesesteak.js @@ -88,11 +88,15 @@ var i; var len; + // Check whether the event we want to emit actually exists if (!this.hasOwnProperty('_events') || evt in this._events === false) { return; } - for (i = 0, len = this._events[evt].length; i < len; i++) { + len = this._events[evt].length; + + // Call all functions that are bound to the event + for (i = 0; i < len; i++) { this._events[evt][i].apply(this, Array.prototype.slice.call(arguments, 1)); } -- cgit v1.2.3