/ Published in: JavaScript
Example showing how using 'this' in a function depends on where 'this' is defined. From book jQuery in Action.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
$(function(){ //Let ID equal a string this.id = 'someID'; //Set function outer to this var outer = this; $('*').each(function(){ /** * Since outer is being pulled in from outside the function, it still * retains the external context to this. Alert will be 'someID' */ alert(outer.id); }); });