Skip to content

Instantly share code, notes, and snippets.

@jaspervalero
Last active August 29, 2015 14:16
Show Gist options
  • Select an option

  • Save jaspervalero/20dda9ee6fc36489d80f to your computer and use it in GitHub Desktop.

Select an option

Save jaspervalero/20dda9ee6fc36489d80f to your computer and use it in GitHub Desktop.
ES6 Lexical this Example
function Gamer() {
// 'this' refers to Gamer
this.playTime = 0;
// We use an ES6 arrow function
setInterval(() => {
// So 'this' still refers to the Gamer object
this.playTime++;
// Correctly outputs and incremented value representing play time in seconds
console.log( this.playTime );
}, 1000 );
}
var gamer = new Gamer();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment