forked from jquery/jquery
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixing $.proxy to work like (and use) Function.prototype.bind (ticket…
… #7783) http://bugs.jquery.com/ticket/7783
- Loading branch information
Showing
3 changed files
with
65 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -740,31 +740,42 @@ jQuery.extend({ | |
// A global GUID counter for objects | ||
guid: 1, | ||
|
||
proxy: function( fn, proxy, thisObject ) { | ||
if ( arguments.length === 2 ) { | ||
if ( typeof proxy === "string" ) { | ||
thisObject = fn; | ||
fn = thisObject[ proxy ]; | ||
proxy = undefined; | ||
|
||
} else if ( proxy && !jQuery.isFunction( proxy ) ) { | ||
thisObject = proxy; | ||
proxy = undefined; | ||
} | ||
// Bind a function to a context, optionally partially applying any | ||
// arguments. | ||
proxy: function( fn, context ) { | ||
var args, proxy; | ||
|
||
// Quick check to determine if target is callable, in the spec | ||
// this throws a TypeError, but we will just return undefined. | ||
if ( ! jQuery.isFunction( fn ) ) { | ||
return undefined; | ||
} | ||
|
||
if ( !proxy && fn ) { | ||
proxy = function() { | ||
return fn.apply( thisObject || this, arguments ); | ||
}; | ||
if ( jQuery.isFunction( Function.prototype.bind ) ) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
// Native bind | ||
args = slice.call( arguments, 1 ); | ||
proxy = Function.prototype.bind.apply( fn, args ); | ||
This comment has been minimized.
Sorry, something went wrong.
fearphage
|
||
} 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 | ||
if ( fn ) { | ||
proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.guid++; | ||
} | ||
proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.guid++; | ||
|
||
// So proxy can be declared as an argument | ||
return proxy; | ||
}, | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
We should probably drop a test for this into jQuery.support, testing for existence on every $.proxy call seems a waste of time....