Skip to content

Instantly share code, notes, and snippets.

@eko24ive
Created April 3, 2017 14:54
Show Gist options
  • Save eko24ive/92b8b8dc34e3b23667c83597be9fa8ae to your computer and use it in GitHub Desktop.
Save eko24ive/92b8b8dc34e3b23667c83597be9fa8ae to your computer and use it in GitHub Desktop.
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