diff options
| author | Juuso Lapinlampi <wub@partyvan.eu> | 2015-11-18 17:24:14 +0100 |
|---|---|---|
| committer | Juuso Lapinlampi <wub@partyvan.eu> | 2015-11-18 17:47:30 +0100 |
| commit | c257a1c1d63197f5349c12962558eccc7837a0f5 (patch) | |
| tree | 5f14ff64d772fdcd4ff9dac1512628428707ce44 | |
| parent | c950e85c8248800f321e280c16a48f15a35e8d4a (diff) | |
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)
| -rw-r--r-- | js/cheesesteak.js | 6 |
1 files changed, 5 insertions, 1 deletions
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)); } |
