Reference: jQuery Proxy Method

Takes an existing function and returns a new one with a particular context.

Parameters

  1. function - The existing function to be called.
  2. context - The object to which the context (this) of the function should be set.
  3. additionalArguments - (Optional) Additional arguments to be passed to the existing function to be called.

Syntax

$.proxy(function, context, additionalArguments);

Example

When the greeting button is clicked, call the person.greeting function and set the context of the function call to the person object.

var person = {
  firstName: "John",
  lastName: "Doe",
  greeting: function () {
    alert("Hello. My name is: " + this.firstName + " " + this.lastName);
  }
}
 
$("#greetingButton").on("click", $.proxy(person.greeting, person));
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License