15 ms·
The counter should be declared as static inside the function, thus limiting is scope and avoiding pollution of the global namespace.
by SpaceNoodled 2y ago
The counter should be declared as static inside the function, thus limiting is scope and avoiding pollution of the global namespace.
- Jtsummers 2y agoIn this case, yes. Its scope should be the lowest necessary scope. Does JS provide static variables in functions? If not, then that forces you to lift it to some other scope like file or module scope or the surrounding function or class if that's viable.
- porridgeraisin 2y ago> Does JS provide static variables const f = (() => { let cnt = 0; return () => console.log("cnt", cnt); })(); :-)