Skip to content

Commit

Permalink
Remove unnecessary usage of Function.prototype.bind (#7783) but maint…
Browse files Browse the repository at this point in the history
…ain API. Also fix bug with proxy failing when a name is provided. Fixes #8893.
  • Loading branch information
jeresig committed Apr 17, 2011
1 parent 2786a11 commit f408aa4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 36 deletions.
40 changes: 9 additions & 31 deletions src/core.js
Expand Up @@ -752,45 +752,23 @@ jQuery.extend({
// Bind a function to a context, optionally partially applying any // Bind a function to a context, optionally partially applying any
// arguments. // arguments.
proxy: function( fn, context ) { proxy: function( fn, context ) {
var args, proxy;

// XXX BACKCOMPAT: Support old string method.
if ( typeof context === "string" ) { if ( typeof context === "string" ) {
fn = fn[ context ]; var tmp = fn[ context ];
context = arguments[0]; context = fn;
fn = tmp;
} }


// Quick check to determine if target is callable, in the spec // Quick check to determine if target is callable, in the spec
// this throws a TypeError, but we will just return undefined. // this throws a TypeError, but we will just return undefined.
if ( ! jQuery.isFunction( fn ) ) { if ( !jQuery.isFunction( fn ) ) {
return undefined; return undefined;
} }


if ( jQuery.support.nativeBind ) { // Simulated bind
// Native bind var args = slice.call( arguments, 2 ),
args = slice.call( arguments, 1 ); proxy = function() {
if ( args.length ) { return fn.apply( context, args.concat( slice.call( arguments ) ) );
proxy = Function.prototype.bind.apply( fn, args ); };
} else {
proxy = fn.bind( context );
}
} else {
// Simulated bind
args = slice.call( arguments, 2 );
if ( args.length ) {
proxy = function() {
return arguments.length ?
fn.apply( context, args.concat( slice.call( arguments ) ) ) :
fn.apply( context, args );
};
} else {
proxy = function() {
return arguments.length ?
fn.apply( context, arguments ) :
fn.call( context );
};
}
}


// Set the guid of unique handler to the same of original handler, so it can be removed // Set the guid of unique handler to the same of original handler, so it can be removed
proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.guid++; proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.guid++;
Expand Down
4 changes: 0 additions & 4 deletions src/support.js
Expand Up @@ -77,10 +77,6 @@ jQuery.support = (function() {
// Test setAttribute on camelCase class. If it works, we need attrFixes when doing get/setAttribute (ie6/7) // Test setAttribute on camelCase class. If it works, we need attrFixes when doing get/setAttribute (ie6/7)
getSetAttribute: div.className !== "t", getSetAttribute: div.className !== "t",


// Test for presence of native Function#bind.
// Not in: >= Chrome 6, >= FireFox 3, Safari 5?, IE 9?, Opera 11?
nativeBind: jQuery.isFunction( Function.prototype.bind ),

// Will be defined later // Will be defined later
submitBubbles: true, submitBubbles: true,
changeBubbles: true, changeBubbles: true,
Expand Down
5 changes: 4 additions & 1 deletion test/unit/core.js
Expand Up @@ -910,7 +910,7 @@ test("jQuery.isEmptyObject", function(){
}); });


test("jQuery.proxy", function(){ test("jQuery.proxy", function(){
expect(6); expect(7);


var test = function(){ equals( this, thisObject, "Make sure that scope is set properly." ); }; var test = function(){ equals( this, thisObject, "Make sure that scope is set properly." ); };
var thisObject = { foo: "bar", method: test }; var thisObject = { foo: "bar", method: test };
Expand All @@ -921,6 +921,9 @@ test("jQuery.proxy", function(){
// Basic scoping // Basic scoping
jQuery.proxy( test, thisObject )(); jQuery.proxy( test, thisObject )();


// Another take on it
jQuery.proxy( thisObject, "method" )();

// Make sure it doesn't freak out // Make sure it doesn't freak out
equals( jQuery.proxy( null, thisObject ), undefined, "Make sure no function was returned." ); equals( jQuery.proxy( null, thisObject ), undefined, "Make sure no function was returned." );


Expand Down

0 comments on commit f408aa4

Please sign in to comment.