Skip to content

Instantly share code, notes, and snippets.

@rwaldron
Forked from getify/gist:6871970
Last active December 24, 2015 22:19
Show Gist options
  • Save rwaldron/6872339 to your computer and use it in GitHub Desktop.
Save rwaldron/6872339 to your computer and use it in GitHub Desktop.
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");
}
}
];
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 = [
{
foo(count) {
if (count < 10) {
let bar = _ => {
if (count % 2 === 0) {
count++;
setTimeout(bar,100);
}
else {
this.foo(count+2);
}
};
setTimeout(bar,100);
}
else {
console.log("foo: " + count);
}
}
}
];
@rwaldron
Copy link
Author

rwaldron commented Oct 7, 2013

http://gul.ly/dhj

Options -> Show All Options -> blockBinding

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment