Created
April 3, 2017 14:54
-
-
Save eko24ive/92b8b8dc34e3b23667c83597be9fa8ae to your computer and use it in GitHub Desktop.
This file contains 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
class TestClass { | |
constructor() { | |
a = 0; | |
} | |
increment() { | |
this.a++; | |
} | |
strangeLoop() { | |
// This will fail | |
[1].forEach(function() { | |
this.a = 1; | |
}) | |
} | |
fixedStrangeLoop() { | |
var self = this; | |
[1].forEach(function() { | |
self.a = 2; | |
}) | |
} | |
arrowLoop() { | |
[1].forEach(() => { | |
this.a = 3; | |
}) | |
} | |
} | |
var x = new TestClass(); | |
x.strangeLoop(); | |
x.fixedStrangeLoop(); | |
x.arrowLoop(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment