Re:What’s New in ES2019
by pault on Tuesday July 30, 2019 @10:28AM
(#20565104)
In my opinion this is a terrible idea, since it's very easy (I do it all the time) to accidentally write `if (foo = bar)` instead of `if (foo == bar)`. If that were valid syntax it would be a huge footgun. I'd be onboard with it if it required a different syntax.
[ Parent
| on HN ]
I'm down if you require a let/var/const in front of it:
if (foo = bar()) { // syntax error!
}
if (let foo = bar()) { // works fine
}
if (const foo = bar()) { // also works fine
}
if (var foo = bar()) { // also also works fine
}> if (foo = bar()) { // syntax error!
That's already an error in strict mode (which would presumably be on for anyone writing bleeding-edge JS).
It's only an error in strict mode if there's no variable in scope named foo.
Re:What’s New in ES2019
by dotancohen on Tuesday July 30, 2019 @12:43PM
(#20566592)
And what if foo had already been defined in the scope?
Re:What’s New in ES2019
by munificent on Tuesday July 30, 2019 @01:25PM
(#20567016)
The new one shadows it.
Re:What’s New in ES2019
by dotancohen on Wednesday July 31, 2019 @08:54AM
(#20574002)
Local scope might not be what the programmer wants, though, depending on circumstance. Pseudocode:
i = 3;
foo = \0;
while ( i-- ) {
if ( foo = resultFromFooApi() ) {
break;
}
sleep 1;
}
return foo;
Re:What’s New in ES2019
by TazeTSchnitzel on Tuesday July 30, 2019 @11:19AM
(#20565634)
But JS already allows that.