Skip to content

Instantly share code, notes, and snippets.

@user24
Created March 28, 2014 15:35
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 user24/9835573 to your computer and use it in GitHub Desktop.
Save user24/9835573 to your computer and use it in GitHub Desktop.
function SomeBuggyInstantiatedObject() {
this.name = "bob";
someElement.onclick = function() {
alert(this.name); // undefined, because this context is the click event
}
}
function SomeSuperCoolInstantiatedObject() {
var that = this;
this.name = "bob";
someElement.onclick = function() {
alert(that.name); // bob, because that is sealed in scope when the function was declared
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment