In the following JavaScript code block I do NOT want to declare a new function f
in the for
-scope but just to assign () => i
to the previously declared let f
, thus creating a closure to the for
-scoped declared and defined variable i
.
Unfortunately the code results in a Type Error: f is not a function
because f = () => i
is being interpreted as let f = () => i
:
{
let f;
for (let i = 'a', f = () => i; i == 'a'; ) {
i = 'b';
}
f();
}
In the for
-loop, how can I separate f = () => i
from the precedent let i = 'a'
?
Putting it in parentheses results in a Syntax Error
:
{
let f;
for ((let i = 'a'), f = () => i; i == 'a'; ) {
i = 'b';
}
f();
}
I don't want to change the scopes. I'm just looking for a syntactic mean to express the exactly scope constellation as given in my question.
I've found the solution:
{
let f;
for (let i = (f = () => i, 'a'); i == 'a'; ) {
i = 'b';
}
f();
}
Or with an additional dummy-helper:
{
let f;
for (let i = 'a', dummy = (f = () => i, 'ignore'); i == 'a'; ) {
i = 'b';
}
f();
}