Skip to content

Instantly share code, notes, and snippets.

@johnmaxwell
Created June 12, 2013 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnmaxwell/5765023 to your computer and use it in GitHub Desktop.
Save johnmaxwell/5765023 to your computer and use it in GitHub Desktop.
// this won't work because "this" doesn't map to this object
// when the click handler fires.
{
setupHandler: function () {
$('button').click(function () {this.handleClicks});
},
handleClicks: function () {
alert("I got clicked");
}
}
// So you have to assign "this" to "that" so that it can
// be bound the click handlers closure.
{
setupHandler: function () {
var that = this;
$('button').click(function () {that.handleClicks});
},
handleClicks: function () {
alert("I got clicked");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment