Skip to content
This repository was archived by the owner on May 17, 2021. It is now read-only.

Commit a707717

Browse files
committed
Merge branch 'release/4.2.2'
2 parents cc1fc01 + 8ab4403 commit a707717

File tree

5 files changed

+42
-11
lines changed

5 files changed

+42
-11
lines changed

EventEmitter.js

+19-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
/*!
2-
* EventEmitter v4.2.1 - git.io/ee
2+
* EventEmitter v4.2.2 - git.io/ee
33
* Oliver Caldwell
44
* MIT license
55
* @preserve
66
*/
77

88
(function () {
9-
// Place the script in strict mode
109
'use strict';
1110

1211
/**
@@ -41,6 +40,19 @@
4140
return -1;
4241
}
4342

43+
/**
44+
* Alias a method while keeping the context correct, to allow for overwriting of target method.
45+
*
46+
* @param {String} name The name of the target method.
47+
* @return {Function} The aliased method
48+
* @api private
49+
*/
50+
function alias(name) {
51+
return function aliasClosure() {
52+
return this[name].apply(this, arguments);
53+
};
54+
}
55+
4456
/**
4557
* Returns the listener array for the specified event.
4658
* Will initialise the event object and listener arrays if required.
@@ -137,7 +149,7 @@
137149
/**
138150
* Alias of addListener
139151
*/
140-
proto.on = proto.addListener;
152+
proto.on = alias('addListener');
141153

142154
/**
143155
* Semi-alias of addListener. It will add a listener that will be
@@ -157,7 +169,7 @@
157169
/**
158170
* Alias of addOnceListener.
159171
*/
160-
proto.once = proto.addOnceListener;
172+
proto.once = alias('addOnceListener');
161173

162174
/**
163175
* Defines an event name. This is required if you want to use a regex to add a listener to multiple events at once. If you don't do this then how do you expect it to know what event to add to? Should it just add to every possible match for a regex? No. That is scary and bad.
@@ -213,7 +225,7 @@
213225
/**
214226
* Alias of removeListener
215227
*/
216-
proto.off = proto.removeListener;
228+
proto.off = alias('removeListener');
217229

218230
/**
219231
* Adds listeners in bulk using the manipulateListeners method.
@@ -367,7 +379,7 @@
367379
/**
368380
* Alias of emitEvent
369381
*/
370-
proto.trigger = proto.emitEvent;
382+
proto.trigger = alias('emitEvent');
371383

372384
/**
373385
* Subtly different from emitEvent in that it will pass its arguments on to the listeners, as opposed to taking a single array of arguments to pass on.
@@ -428,7 +440,7 @@
428440
return EventEmitter;
429441
});
430442
}
431-
else if (typeof module !== 'undefined' && module.exports){
443+
else if (typeof module === 'object' && module.exports){
432444
module.exports = EventEmitter;
433445
}
434446
else {

EventEmitter.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bower.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "eventEmitter",
33
"description": "Event based JavaScript for the browser",
4-
"version": "4.2.1",
4+
"version": "4.2.2",
55
"main": [
66
"./EventEmitter.js"
77
],

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "wolfy87-eventemitter",
3-
"version": "4.2.1",
3+
"version": "4.2.2",
44
"description": "Event based JavaScript for the browser",
55
"main": "EventEmitter.js",
66
"directories": {

tests/tests.js

+19
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,25 @@
726726
});
727727
});
728728

729+
suite('alias', function () {
730+
test('that it works when overwriting target method', function () {
731+
var addListener = EventEmitter.prototype.addListener;
732+
var res;
733+
var rand = Math.random();
734+
735+
EventEmitter.prototype.addListener = function () {
736+
res = rand;
737+
};
738+
739+
var ee = new EventEmitter();
740+
ee.on();
741+
742+
assert.strictEqual(res, rand);
743+
744+
EventEmitter.prototype.addListener = addListener;
745+
});
746+
});
747+
729748
// Execute the tests.
730749
mocha.run();
731750
}.call(this));

0 commit comments

Comments
 (0)