An array in js is just an object/dictionary, with a special 'length' property, and a few convenience methods.
Further, the 'length' property can also be confusing to newcomers, since it doesn't relate to the number of elements in the array, rather the maximum key that can be converted to an integer +1.
For example,
var arr = []; arr.foo = "hello"; arr[4] = 56;
// arr.length = 5
It's much easier when you understand that arrays in javascript don't really exist as a separate data type. They are, as I say, just objects with a couple of convenience methods+length property.
Sure, x.a should stringize 'a' to make property access work cleanly, but why when using the indexing syntax?
For example:
sgraham@gerard:~$ lua
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
> a = {}
> a[0] = "wee"
> a[1] = "waa"
> a["1"] = "str"
> print(a[0])
wee
> print(a[1])
waa
> print(a["1"])
str
>
> a["x"] = "wee"
> a.x = "waa"
> print(a["x"])
waa
> print(a.x)
waa
>
which seems wholly more reasonable.[] syntax is for objects and arrays. As I've said, an array is just an object... Same data type.
If you didn't have a [] syntax, you wouldn't be able to use a variable to reference properties on an object.
var baz = "foo";
foo.bar == foo["bar"] == foo[baz]It makes it clear for trivial operations. Once you enter the realm of nested hashes (preferably with some arrays mixed in) the syntax turns into a royal pain in the ass.
Perl has implicit type conversions similar to those of JavaScript, but the syntax of the dereference operation makes it clear what's going on:
Even without the obvious variable names, you can tell from the syntax what is going to be converted to a number and what is going to be converted to a string; [] means number and {} means string.Javascript is stuck with the same syntax for both:
which is apparently somewhat confusing. I know what is going to happen, but the language doesn't help me; it's easy to write code that's misleading.