4 ms·
Ecmascript had the same problem, and fixed it without breaking backwards compatibility. In javascript, for (var i=0; i<3; i++) setTimeout(() => console.log
by lovasoa 4y ago
Ecmascript had the same problem, and fixed it without breaking backwards compatibility. In javascript,
for (var i=0; i<3; i++) setTimeout(() => console.log(i))
Would print 3,3,3.
So in ecmascript 6, blocked-scope variables were introduced, but the semantics of old-style "var" declarations was not changed. You can now write
for (let i=0; i<3; i++) setTimeout(() => console.log(i))
Which prints 0,1,2 as expected.
And you don't have to go open a `go.mod` file to know what the code you are reading does.
- dilap 4y agoRight call for js, but I think not for C# or Go, since as time goes on, the old behavior will be more and more of a relic, but you'll be paying the cost for having two syntaxes forever. (In js tho you have no alternative, because you don't have anything like go.mod (I think?) and you really can't break backcompat.)