I was reading the api about jQuery.proxy(). It looks promising but I was wondering in what situation is this best use. Can anyone enlighten me?
I was reading the api about jQuery.proxy() . It looks promising but I was
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
When you want a function that has the
thisvalue bound to a specific object. For example, in callbacks such as event handlers, AJAX callbacks, timeouts, intervals, custom objects, etc.This is just a manufactured example of a situation where it might be useful. Assuming there is a
Personobject which has a property name. It is also linked to a text input element, and whenever the input value changes, the name in this person object gets updated too.One solution that we often use is to store the this context in a variable and use that inside the callback function such as:
Alternatively, we could have used
jQuery.proxyhere so the reference tothisrefers to the object of Person instead of the element that triggered the event.Note that this feature has been standardized into ECMAScript 5 which now includes the
bindmethod borrowed from prototypejs and is already available on some browsers.