/documentation/detail/$.proxy.md
https://github.com/maoyao/appframework · Markdown · 24 lines · 15 code · 9 blank · 0 comment · 0 complexity · f3c66972f9eb02b06fec0f1dcc1a4ab4 MD5 · raw file
- $.proxy(callback,context) allows you to create a proxy function that changes the context of "this"
- There are times where you want "this" to be something other then the object that the event or function is dispatched on.
- ```js
- var newObj={foo:bar}
- $("#main").bind("click",$.proxy(function(evt){console.log(this)},newObj);
- or
- ( $.proxy(function(foo, bar){console.log(this+foo+bar)}, newObj) )('foo', 'bar');
- or
- ( $.proxy(function(foo, bar){console.log(this+foo+bar)}, newObj, ['foo', 'bar']) )();
- ```
- Below we will have an anchor and proxy the click event so "this" is the object {foo:'bar'}
- ```js
- var obj={foo:'bar'}
- $("#proxyTest").bind("click",$.proxy(function(){alert(this.foo);},obj));
- ```