Say I have the following code:
[ { x: 1 }, { x: 2 }, { x: 3 } ]
> for (let { x } of a) console.log(x)
1
2
3
which makes sense. But what if I did:
> a = (x) => [{"x": x + 1}, {"x": x + 2}, {"x": x + 3}]
> x = 0
0
> for (let { x } of a(x)) console.log(x)
ReferenceError: x is not defined
> x
0
Why I got a ReferenceError
? a(x)
shouldn't be in the let
's scope so x
should be propagating upwards.
I know that the problem is coming from let
, not for...of
.
> x = 1
1
> f = x => x + 1;
[Function: f]
> let x = f(x)
ReferenceError: x is not defined
If the scope of let
in the example above is global, then I shouldn't be getting a ReferenceError
!