Last active
August 29, 2015 14:16
-
-
Save jaspervalero/20dda9ee6fc36489d80f to your computer and use it in GitHub Desktop.
ES6 Lexical this Example
This file contains hidden or 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
| 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