News for hackers. Stuff that matters.
Re:Silly Javascript by jrockway on Saturday July 25, 2009 @05:03PM (#723666)
This is the danger of syntax minimalism; using the same operator to refer to elements of both arrays and dictionaries.

Perl has implicit type conversions similar to those of JavaScript, but the syntax of the dereference operation makes it clear what's going on:

    $array[$number] $array[0] $array['0']
    $hash{$key} $hash{'42'} $hash{42}
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:

    array[number] array[42] array["42"]
    hash[key] hash[42] hash["42"]
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.
Re:Silly Javascript by axod on Saturday July 25, 2009 @05:09PM (#723681)
Sorry, but that's wrong. I don't think there's any ambiguity at all. JS only has string keys, and only has objects.

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.

Re:Silly Javascript by s3graham on Saturday July 25, 2009 @05:35PM (#723713)
I think I agree with jrockway. I understand (now) that there's only string keys in objects, but that's the random part. Why have [] syntax for "arrays" then? It's the hashing behaviour that's the 'gotcha'.

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.
Re:Silly Javascript by axod on Saturday July 25, 2009 @05:39PM (#723721)
>> Why have [] syntax for "arrays"

[] 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]
Re:Silly Javascript by jrockway on Saturday July 25, 2009 @07:33PM (#723836)
So we're back to my main argument -- minimalism is confusing.
Re:Silly Javascript by axod on Saturday July 25, 2009 @08:20PM (#723870)
If you're more used to complexity I guess :/
Re:Silly Javascript by moe on Saturday July 25, 2009 @08:42PM (#723894)
but the syntax of the dereference operation makes it clear what's going on

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.