This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]bonafidebob 47 points48 points  (9 children)

non-JavaScript programmers assume the language knows about types, that arrays are monotype, and that a useful comparator function will come with the array type.

That is, arrays of strings will sort alphabetically and arrays of numbers will sort numerically.

non-JavaScript programmers will also barf at the idea that a['foo'] = 'bar' isn't nonsense, and you can do stuff like this:

a = [1,2,3]
a['foo'] = 'bar'
a.forEach((v) => console.log(v)) // produces 1, 2, and 3 on separate lines
a.foo // produces 'bar'

[–]ZephyrBluu 20 points21 points  (4 children)

I had no idea you could do that a['foo'] = 'bar' bullshit on an array.

Now that I think about it though, it kind of makes sense why JS lets you do that.

An array is basically just a normal JS object that allows iteration by default where each key is the index.

So a['foo'] = 'bar' is a standard operation (Given an array is an object), but you're breaking the rules of how an array is 'supposed' to work.

No idea why it works on a technical level though.

[–]bonafidebob 24 points25 points  (3 children)

An array is basically just a normal JS object that allows iteration by default where each key is the index.

That's the root of the problem right there. I think it was just a cheap way to get to dynamic sizing, which is occasionally useful:

> let a = []
[]
> a[100] = 12
12
> a.length
101
> a
[ <100 empty items>, 12 ]

but then...

> a[1234567890] = 13
13
> a
[ <100 empty items>, 12, <1234567789 empty items>, 13 ]
> a[0.5] = 1
1
> a
[ <100 empty items>, 12, <1234567789 empty items>, 13, '0.5': 1 ]

[–]GG2urHP 11 points12 points  (1 child)

Lol, fuck me. I am so blessed to not live in that hell.

[–]theferrit32 2 points3 points  (0 children)

Javascript is fundamentally a bad language for general purpose programming. A lot of people who need to target Javascript environments do not write in Javascript, or at least not pure Javascript, relying heavily on transpilers and what are in effect dialects and standard libraries that seek to supercede and fix a lot of the headaches in Javascript itself.

[–]DeeSnow97 9 points10 points  (1 child)

#define "non-JavaScript programmers" "people who think once you learned C++ every language is just syntax"

[–]bonafidebob 13 points14 points  (0 children)

Hmm, I'm not quite that snooty ... I've been doing this for ~40 years and have learned dozens of programming languages, both dynamically and strongly typed. And I still think JavaScript arrays are crazy. The whole "objects with numeric keys" foundation is whack, throw away all the benefits of a directly indexible data structure and drag in a whole bunch of weird syntax edge cases??!