English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
$.proxy() method accepts an existing function and returns a new function with a specific context. This method is usually used to add events to elements pointing to different objects in the context.
jQuery.proxy(function, context)
jQuery.proxy(context, name)
To enforce the context of the "getFullName" function within myObj:
let myObj = { fname: "Seagull", lname: "Anna", age: 22, getFullName: function(){ $("p").after("First Name: " + this.fname + "<br>Last Name: " + this.lname); } }; $("button").click($.proxy(myObj, "getFullName"));Test to see‹/›
Parameter | Description |
---|---|
function | The function whose context will change |
context | The object to which the function's context (this) should be set |
name | Rename the function that changes its context (should be the property of the context object) |