Skip to content

Instantly share code, notes, and snippets.

@savelee
Last active July 25, 2016 13:56
Show Gist options
  • Save savelee/eba59a147afd70b2e50226555596e696 to your computer and use it in GitHub Desktop.
Save savelee/eba59a147afd70b2e50226555596e696 to your computer and use it in GitHub Desktop.
Lexical This
//ES2015
function VideoGame(){
this.title = "Uncharted";
this.version = 3;
setInterval(() => {
console.log(this.title + " " + this.version++); // |this| properly refers to the VideoGame object
}, 5000);
}
//prior ES2015
function VideoGame(){
var me = this;
me.title = "Uncharted";
me.version = 3;
setInterval(function() {
// The callback refers to the `me` variable of which
// the value is the expected object.
console.log("1:", this); //Context is Window
console.log("2:", me); //Context is VideoGame object
console.log(me.title + " " + me.version++);
}, 5000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment