rse / es6-features

ECMAScript 6: Feature Overview & Comparison

Home Page: https://rse.github.io/es6-features/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add block-scope callback expressions example

webbedspace opened this issue · comments

commented

By far one of the most popular applications of let is the ability to do this:

let callbacks = [];
for(let i = 0; i <= 2; i++) {
    callbacks[i] = function() { return i * 2; };
}
callbacks[0]() === 0;
callbacks[1]() === 2;
callbacks[2]() === 4;

That is, each function has its own binding of i, rather than a shared function-scoped binding.
(This is not supported by any browsers yet.)

Good catch! And good example. Now added (with the ES5 equivalent), too. Thanks for the feedback.