PageRenderTime 42ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/documentation/detail/$.proxy.md

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