Last active
December 24, 2015 22:19
-
-
Save getify/6871970 to your computer and use it in GitHub Desktop.
the following patterns are "worse" without a lexical name binding for the concise method sugar of ES6
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
var a = [ | |
{ | |
// Look, ma, no `this` | |
foo: function foo() { | |
setTimeout(foo,100); | |
console.log("foo"); | |
} | |
} | |
]; | |
var b = [ | |
{ | |
// shorthand method syntax is nice, but introduces `this` issues | |
// because of lack of lexical name binding | |
foo() { | |
setTimeout(this.foo,100); // ** uglier this.foo | |
console.log("foo"); | |
} | |
} | |
]; |
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
var a = [ | |
{ | |
// Look, ma, no `this` | |
foo: function foo(count) { | |
if (count < 10) { | |
setTimeout(function bar(){ | |
if (count % 2 === 0) { | |
count++; | |
setTimeout(bar,100); | |
} | |
else { | |
foo(count+2); | |
} | |
},100); | |
} | |
else { | |
console.log("foo: " + count); | |
} | |
} | |
} | |
]; | |
var b = [ | |
{ | |
// shorthand method syntax is nice, but introduces `this` issues | |
// because of lack of lexical name binding | |
foo(count) { | |
if (count < 10) { | |
// `bar` can't be => here because we need a name for it | |
// to refer to itself | |
setTimeout(function bar(){ | |
if (count % 2 === 0) { | |
count++; | |
setTimeout(bar.bind(this),100); // ** uglier .bind(this) | |
} | |
else { | |
this.foo(count+2); // ** uglier this.foo | |
} | |
}.bind(this),100); // ** uglier .bind(this) | |
} | |
else { | |
console.log("foo: " + count); | |
} | |
} | |
} | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment