News for hackers. Stuff that matters.
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 :/