tadam logo
Did you find an error in the text?
Select this with mouse and press
Ctrl + Enter
Xhtml.co.il Check Spelling
Orphus system

jQuery.proxy()

jQuery.proxy( function, context )

Returns: Function
Description: Takes a function and returns a new one that will always have a particular context.

Arguments

jQuery.proxy( function, context )

version added: 1.4
function
The function whose context will be changed.
context
The object to which the context (`this`) of the function should be set.

jQuery.proxy( context, name )

version added: 1.4
context
The object to which the context of the function should be set.
name
The name of the function whose context will be changed (should be a property of the 'context' object.
This method is most useful for attaching event handlers to an element where the context 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 context 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 ) );
Was this information helpful?
   

Comments