On Wed, Mar 21, 2018 at 1:27 PM, Sebastian Malton <[email protected]>
wrote:

> Because block-level scoping is a very good way to avoid certain bugs and
> is easier to reason about. Especially when considering project successors.
>

+1.  function-scoped variables in loop bodies caused tons of bugs before
let-scoped variables and were a main motivating case.

var i;
for (i = 0; i < arr.length; ++i) {
  f(function () { /* Do something with */ arr[i]; });
}

vs

for (let i = 0; i < arr.length; ++i) {
  f(function () { /* Do something with */ arr[i]; });
}

Yes, linters got pretty good at finding uses of closed-over variables
modified in a loop, but the workarounds were not ideal.

var i;
for (i = 0; i < arr.length; ++i) {
  f(function (i) { return function () { /* Do something with */ arr[i]; }
}(i));
}

Block scoping is just better for code that uses loops, variables, and
function expressions.
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to