News for hackers. Stuff that matters.
Re:Silly Javascript by invisible on Saturday July 25, 2009 @04:51PM (#723661)
x is an Array (which should only accept numerics for keys). E.g. you cannot have a String be a "key" in an Array.

However, for an Object (if they used {} instead of []), the property gets converted using String() before being assigned, so you cannot have duplicate values with the same String value. This actually makes sense when you consider that JavaScript isn't a hard typed language and you often don't need to type cast (which comes in handy more often than it fails a programmer).

My opinion: this guy is complaining just to complain, he could throw a _ before that 1 String and it solves his type problem completely.

Re:Silly Javascript by axod on Saturday July 25, 2009 @05:47PM (#723734)
>> "x is an Array (which should only accept numerics for keys). E.g. you cannot have a String be a "key" in an Array."

False. You can have anything you want in an Array, since it's just an object.

  var arr = [];
  arr["hello"] = 57;
  arr.world = function() {};
  arr[999.99] = "flibble!";
  arr[54] = 9;
  // arr.length now equals 55
May or may not be a good coding style, but that's a separate matter.
Re:Silly Javascript by invisible on Sunday July 26, 2009 @01:27AM (#724086)
Agreed, just in a rush to comment before. I'd go one further and say using an Array that way is just plain worrisome in JavaScript. It just breaks the conceptual usage "sort of" of Array vs Object (for/while vs for each...in). That's why a lot of frameworks add an "each" iterator for Array/Hashes (Hashes being a "controlled" Object added by most frameworks).
Re:Silly Javascript by s3graham on Saturday July 25, 2009 @05:16PM (#723689)
I get the impression you don't know JS any better than I do.

[] and {} seem to be identical, except that [] one auto-increments a key-as-string in its initialization syntax.

Yes, an _ before the string would fix that particular interactive session, but it's not going to work in general.

Re:Silly Javascript by invisible on Saturday July 25, 2009 @05:44PM (#723732)
[] creates an Array (a special instance of an Object which has a completely different prototype). {} creates an Object with no special methods except ones that apply to all types in JavaScript. I'm pretty good at JS - I swear.

In general, most don't use the same String value for a key/index to store different values. Just my opinion on that one, though.

Oh, and: you can use Firebug to effectively see the difference between the two.

  >>> x = ["1"];
  ["1"]
  >>> x = {"1": "1"};
  Object 1=1
Re:Silly Javascript by s3graham on Saturday July 25, 2009 @06:17PM (#723764)
> I'm pretty good at JS - I swear.

Sorry, didn't mean to be a dick. :) This particular wart apparently got my nerd rage going.

> In general, most don't use the same String value for a key/index to store different values

Agreed, it'd generally be pretty sketchy. My current puttering around project is a "Python-in-JS" in the vein of http://objective-j.org/ which is where the desire for a "map" rather than an "object" comes from.