jQuery.proxy()
jQuery.proxy( function, scope ) Returns: Function
Description: Takes a function and returns a new one that will always have a particular scope.
-
version added: 1.4jQuery.proxy( function, scope )
functionThe function whose scope will be changed.
scopeThe object to which the scope of the function should be set.
-
version added: 1.4jQuery.proxy( scope, name )
scopeThe object to which the scope of the function should be set.
nameThe name of the function whose scope will be changed (should be a property of the 'scope' object.
This method is most useful for attaching event handlers to an element where the scope is pointing back to a different object. Additionally, jQuery makes sure that even if you bind the function returned from jQuery.proxy() it will still unbind the correct function, if passed the original.
Example:
Enforce the scope of the function.
var obj = {
name: "John",
test: function() {
alert( this.name );
$("#test").unbind("click", obj.test);
}
};
$("#test").click( jQuery.proxy( obj, "test" ) );
// This also works:
// $("#test").click( jQuery.proxy( obj.test, obj ) );
