We Recommend

Pro JavaScript Techniques Pro JavaScript Techniques
Pro JavaScript Techniques is the ultimate JavaScript book for the modern web developer. It provides everything you need to know about modern JavaScript, and shows what JavaScript can do for your web sites. This book doesn't waste any time looking at things you already know, like basic syntax and structures.


Posted By

1man on 03/05/08


Tagged

this closure execution


Versions (?)


Closure Example Relating to 'this' Execution Context


Published in: JavaScript 


Example showing how using 'this' in a function depends on where 'this' is defined. From book jQuery in Action.

  1. $(function(){
  2. //Let ID equal a string
  3. this.id = 'someID';
  4. //Set function outer to this
  5. var outer = this;
  6. $('*').each(function(){
  7. /**
  8.   * Since outer is being pulled in from outside the function, it still
  9.   * retains the external context to this. Alert will be 'someID'
  10.   */
  11. alert(outer.id);
  12. });
  13. });

Report this snippet 

You need to login to post a comment.