News for hackers. Stuff that matters.
Re:JavaScript Structs by dunham on Thursday October 10, 2024 @06:13PM (#41804107)
> there's absolutely no reason to use var any more.

So I also thought. And then I recently learned that typescript uses `var` internally for performance.

From src/compiler/checker.ts:

    // Why var? It avoids TDZ checks in the runtime which can be costly.
    // See: https://github.com/microsoft/TypeScript/issues/52924
    /* eslint-disable no-var */
    var deferredDiagnosticsCallbacks: (() => void)[] = [];
Re:JavaScript Structs by nsonha on Thursday October 10, 2024 @07:13PM (#41804525)
I can think of a few when hoisting is nice, stylistically:

if (...) var x = ...

else x = ...

/////

try { var x = ...}

catch (error) { x = ... }

/////

for (...) {

  var x: NodeJS.Dict<any> = {}

  x[key] = ...
}

return x

Re:JavaScript Structs by afavour on Thursday October 10, 2024 @07:23PM (#41804584)
All of those feel like anti patterns to me. Much more difficult to read.
Re:JavaScript Structs by nsonha on Thursday October 10, 2024 @08:12PM (#41804866)
That's subjective, Idk about "MUCH more difficult".

All it does is moving the declaration to the correct visual scope, instead of a dangling up-front declaration.

Admittedly, I understand most coders are aready trained to read the latter.

Re:JavaScript Structs by wruza on Friday October 11, 2024 @05:30AM (#41807843)
The worst anti-pattern here is the catch- and finally-blocks living in a different scope.

Really, who thought it was a good idea that finalization and error handling blocks must have no access to their subject scope? Every damn language copies that nonsense, except for js and its `var` hoisting.

Re:JavaScript Structs by goodoldneon on Friday October 11, 2024 @09:09AM (#41809159)
If performance is so important for your app that `var` is causing issues then JavaScript is likely the wrong language