Skip to content

Commit

Permalink
Fixes ccampbell#14 - Calling with new works the same as without
Browse files Browse the repository at this point in the history
  • Loading branch information
wesleytodd committed Nov 6, 2014
1 parent 083833d commit 738c64f
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions gator.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,29 +302,26 @@
*
* @param {Node} element
*/
function Gator(element, id) {

// called as function
if (!(this instanceof Gator)) {
// only keep one Gator instance per node to make sure that
// we don't create a ton of new objects if you want to delegate
// multiple events from the same node
//
// for example: Gator(document).on(...
for (var key in _gatorInstances) {
if (_gatorInstances[key].element === element) {
return _gatorInstances[key];
}
function Gator(element) {
// only keep one Gator instance per node to make sure that
// we don't create a ton of new objects if you want to delegate
// multiple events from the same node
//
// for example: Gator(document).on(...
for (var id in _gatorInstances) {
if (_gatorInstances[id].element === element) {
return _gatorInstances[id];
}
}

_id++;
_gatorInstances[_id] = new Gator(element, _id);

return _gatorInstances[_id];
// Make sure we are always called with new
if (!(this instanceof Gator)) {
return new Gator(element);
}

this.element = element;
this.id = id;
this.id = _id++;
_gatorInstances[this.id] = this;
}

/**
Expand Down

0 comments on commit 738c64f

Please sign in to comment.