ohmygoodnessihatejavascript

February 24, 2009 at 09:13 PM | Uncategorized | View Comments

Ug.

>>> x = new Object();
>>> x["a"] = 42;
>>> "a" in x
true
>>> x["a"]
42
>>>

Ok, good and sane so far...

>>> y = new Object();
>>> x[y] = 42
>>> y in x
true
>>> x[y]
42
>>>

Alright, that's still pretty sane...

>>> y.name = 'Joe';
>>> y.toString = function () { return this.name; };
>>> x[y] = 42
>>> y in x
true
>>> y.name = 'Bob'
>>> y in x
false

Wait... WHAT?!

Yes, you have it folks... Javascript can't actually hash objects into dictionaries -- it uses their toString function. Pretty unique and immutable, 'eh?

>>> y.name = 'Bob'
>>> y.toString = function () { return this.name; };
>>> x[y] = 42
>>> x['Bob'] = 16
>>> x[y]
16

Ug. Javascript loses. Hard :-(