Created
June 12, 2011 03:50
-
-
Save brikis98/1021233 to your computer and use it in GitHub Desktop.
JavaScript this keyword, that, and callbacks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
this.foo = 'bar'; | |
console.log(this.foo); // prints 'bar' | |
var that = this; // save a reference | |
$('#myButton').click(function(event) { | |
console.log(this.foo); // prints 'undefined' since it's not the same 'this' | |
console.log(that.foo); // prints 'bar' | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See CoffeeScript: JavaScript that’s easy on the eyes for more information.