jQuery.proxy()

Takes a function and returns a new one that will always have a particular context.

jQuery.proxy(function(), context)🡢 Function

function() FunctionThe function whose context will be changed.
context PlainObjectThe object to which the context (this) of the function should be set.

jQuery.proxy(context, name)🡢 Function

context PlainObjectThe object to which the context of the function should be set.
name StringThe name of the function whose context will be changed (should be a property of the context object).

jQuery.proxy(function(), context, additionalArguments)🡢 Function

function() FunctionThe function whose context will be changed.
context PlainObjectThe object to which the context (this) of the function should be set.
additionalArguments AnythingAny number of arguments to be passed to the function referenced in the function argument.

jQuery.proxy(context, name, additionalArguments)🡢 Function

context PlainObjectThe object to which the context of the function should be set.
name StringThe name of the function whose context will be changed (should be a property of the context object).
additionalArguments AnythingAny number of arguments to be passed to the function named in the name argument.

Note: This API has been deprecated in jQuery 3.3; please use the native Function.prototype.bind method instead.

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.

Be aware, however, that jQuery's event binding subsystem assigns a unique id to each event handling function in order to track it when it is used to specify the function to be unbound. The function represented by jQuery.proxy() is seen as a single function by the event subsystem, even when it is used to bind different contexts. To avoid unbinding the wrong handler, use a unique event namespace for binding and unbinding (e.g., "click.myproxy1") rather than specifying the proxied function during unbinding.

As of jQuery 1.6, any number of additional arguments may be supplied to $.proxy(), and they will be passed to the function whose context will be changed.

As of jQuery 1.9, when the context is null or undefined the proxied function will be called with the same this object as the proxy was called with. This allows $.proxy() to be used to partially apply the arguments of a function without changing the context.

Change the context of functions bound to a click handler using the "function, context" signature. Unbind the first handler after first click.

JS
<p><button type="button" id="test">Test</button></p>
<div id="log"></div>
HTML
var me = {
  type: "zombie",
  test: function (event) {
    // Without proxy, `this` would refer to the event target
    // use event.target to reference that element.
    var element = event.target;
    $(element).css("background-color", "red");

    // With proxy, `this` refers to the me object encapsulating
    // this function.
    $("#log").append("Hello " + this.type + "<br>");
    $("#test").off("click", this.test);
  },
};

var you = {
  type: "person",
  test: function (event) {
    $("#log").append(this.type + " ");
  },
};

// Execute you.test() in the context of the `you` object
// no matter where it is called
// i.e. the `this` keyword will refer to `you`
var youClick = $.proxy(you.test, you);

// attach click handlers to #test
$("#test")
  // this === "zombie"; handler unbound after first click
  .on("click", $.proxy(me.test, me))

  // this === "person"
  .on("click", youClick)

  // this === "zombie"
  .on("click", $.proxy(you.test, me))

  // this === "<button> element"
  .on("click", you.test);
DEMO

Enforce the context of the function using the "context, function name" signature. Unbind the handler after first click.

JS
<p><button id="test">Test</button></p>
<p id="log"></p>
HTML
var obj = {
  name: "John",
  test: function () {
    $("#log").append(this.name);
    $("#test").off("click", obj.test);
  },
};
$("#test").on("click", jQuery.proxy(obj, "test"));
DEMO

Change the context of a function bound to the click handler,

JS
<p><button type="button" id="test">Test</button></p>
<div id="log"></div>
HTML
var me = {
  // I'm a dog
  type: "dog",

  // Note that event comes *after* one and two
  test: function (one, two, event) {
    $("#log")
      // `one` maps to `you`, the 1st additional
      // argument in the $.proxy function call
      .append("<h3>Hello " + one.type + ":</h3>")

      // The `this` keyword refers to `me`
      // (the 2nd, context, argument of $.proxy)
      .append("I am a " + this.type + ", ")

      // `two` maps to `they`, the 2nd additional
      // argument in the $.proxy function call
      .append("and they are " + two.type + ".<br>")

      // The event type is "click"
      .append("Thanks for " + event.type + "ing.")

      // The clicked element is `event.target`,
      // and its type is "button"
      .append("the " + event.target.type + ".");
  },
};

var you = { type: "cat" };
var they = { type: "fish" };

// Set up handler to execute me.test() in the context
// of `me`, with `you` and `they` as additional arguments
var proxy = $.proxy(me.test, me, you, they);

$("#test").on("click", proxy);
DEMO

Looking for a Web Developer?

👋

Hi! I'm Basti, author of this site. If you are looking for a web developer with 15+ years of experience, holla at me!

Be it the good 'ol jQuery, vanilla JS or modern frameworks like Vue and Svelte, front- or backend, I can help you.

Just write me at jobs@jqapi.com :)