Skip to content

Commit

Permalink
Optimize Function#bind and Function#bindAsEventListener to avoid usin…
Browse files Browse the repository at this point in the history
…g Array#concat when only the context argument is given. [#215 state:resolved]
  • Loading branch information
savetheclocktower committed Jul 17, 2008
1 parent 4b2913c commit 76e6f9f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
@@ -1,3 +1,5 @@
* Optimize Function#bind and Function#bindAsEventListener to avoid using Array#concat when only the context argument is given. [kangax]

* Ensure Selector.handlers.id finds the proper element even when it's not attached to the document. [jddalton]

* Fix Position.within in IE. [jddalton]
Expand Down
22 changes: 18 additions & 4 deletions src/base.js
Expand Up @@ -168,16 +168,30 @@ Object.extend(Function.prototype, {
bind: function() {
if (arguments.length < 2 && Object.isUndefined(arguments[0])) return this;
var __method = this, args = $A(arguments), object = args.shift();
return function() {
return __method.apply(object, args.concat($A(arguments)));

// Avoid using Array#concat when only the context argument is given.
if (args.length) {
return function() {
return __method.apply(object, args.concat($A(arguments)));
};
}
return function() {
return __method.apply(object, arguments);
};
},

bindAsEventListener: function() {
var __method = this, args = $A(arguments), object = args.shift();
return function(event) {
return __method.apply(object, [event || window.event].concat(args));

// Avoid using Array#concat when only the context argument is given.
if (args.length) {
return function(event) {
return __method.apply(object, [event || window.event].concat(args));
};
}
return function(event) {
return __method.apply(object, [event || window.event]);
};
},

curry: function() {
Expand Down

0 comments on commit 76e6f9f

Please sign in to comment.