Return to Snippet

Revision: 5381
at March 5, 2008 17:32 by 1man


Initial Code
$(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);
	});
});

Initial URL

                                

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

Initial Title
Closure Example Relating to 'this' Execution Context

Initial Tags

                                

Initial Language
JavaScript