2020-10-22

mindstalk: (angry sky)
New Job has me learning JS and Node.js. I have learned that some of my earlier complaints (see tag) have been improved by later developments (or ones extant at the time that I hadn't found.) Like 'var' variables have function scope (useful concept; I think it applies to Python), but you can get block scope (One True Way) by using 'let' instead. I don't think I knew that simply assigning to an undeclared variable creates a global (dear gods) but JS borrowed 'use strict'; from Perl, which shuts that off.

JS strikes me as a shitty half-assed core language which is trying to grow its way into respectability. Unfortunately it has 25 years of shitty web code to have to be backwardly compatible with.

No wonder there are multiple languages to use instead, that compile down into JS.

Today I accidentally discovered another WTF.

> l=[1,2,3]
[ 1, 2, 3 ]
> for (v in l) console.log(typeof(v));
string
string
string
undefined
> for (v in l) console.log(v);
0
1
2


So in a way this makes sense. "for in" iterates over the properties/keys of an object, which for an Object (dictionary-ish) are names. The properties of an Array are numbers, but in a 'for in' context, they could be strings.

OTOH, stripped of rationalization: the indices of an array are numeric, and yet they're strings in this iteration case. ffffuuuuu.

Of course, JS coerces between numbers and strings at least as fluidly as Perl does.

I found this behavior by accident, I was printing 'v[0]' thinking I was using 'for of" (which iterates over the values of an Array) when I wasn't, and I got numbers rather than letters or 'undefined', which is what you get if you try

> n=4
4
> n[0]
undefined

Profile

mindstalk: (Default)
mindstalk

June 2025

S M T W T F S
123 45 67
89 10 1112 1314
15161718192021
222324 25262728
2930     

Page Summary

Most Popular Tags

Expand Cut Tags

No cut tags

Style Credit

Page generated 2025-07-06 08:09
Powered by Dreamwidth Studios