4 ms·
> Javascript have combined array/dict? How come? Ha! Arrays in JS are just objects disguised as arrays, with a fancy prototype and some syntax sugar: no more,
by kolme 12y ago
> Javascript have combined array/dict? How come?
Ha! Arrays in JS are just objects disguised as arrays, with a fancy prototype and some syntax sugar: no more, no less. There are no _real_ arrays in JS (in the traditional sense).
Keys in JS arrays are _strings_, weird as it is, because arrays are just objects, and objects use strings as keys. Try this yourself:
for (var x in ['a']) console.log(x, typeof(x));
Specification may (or may not) clear things up:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.4 http://www.ecma-international.org/ecma-262/5.1/#sec-15.4
Also you can try this out:
['a'][0] // = 'a'
['a']['0'] // = 'a'
['a']['00'] // = undefined
Numbers are coerced into strings, and not the other way around! (Note that I said 'number' on purpose, since integers are available but rarely used on the web, for BC. So it's usually floats coerced into strings every time you do ['a'][0]).
This weird coertions are not much unlike PHP's, which turns anything you give it as a key into a hash, internally. Though PHP arrays are far more complex, and even weirder (like how are they're not passed as a reference or as copy, but kinda both).