Jeremy Bowers wrote:
> On Tue, 15 Mar 2005 03:21:19 -0800, George Jempty wrote:
> > I'm noticing that Javascript's array/"hash" literal syntax is
EXACTLY the
> > same as that for Python lists/dictionaries.
>
> No it isn't, quite.
>
> Two differences of note, one literally syntax and one technically not
but
> you probably still want to know about it.
>
> First, Javascript objects can only use strings for keys, anything
used as
> a key will be converted to a string. Try this in your browser and
you'll
> see what I mean... the "instance" of the "class" I define (let's not
get
> into prototyping issues here :-) ) has its string value used as the
key,
> not the object:

Perhaps the above assertion applies to native Javascript objects only?!
 Because I've been successful in using host(browser) objects as keys to
associative arrays, namely forms.  This is handy when the form's id
and/or name attribute have not been set.  I was concerned that
identical forms would be equal, but then I proved that was not the case
with the following test:

<form></form><form></form>
<form>
<input type="submit" onclick="alert(document.forms[0] ==
document.forms[1])">
</form>

This results in false.  Am cross-posting the rest of this message in
it's entirety to comp.lang.javascript.

> javascript:function a(){}; a.prototype.toString = function () {return
> 'q';}; b = new a(); c = {}; c[b] = 1; alert(c['q'])
>
> (All one line, if your browser objects to the newline.)
>
> The other is the syntax point: The strings you use in {} expressions
to
> denote keys are used literally, they are not resolved. Thus, in the
above
> I *had* to write
>
> c = {};
> c[b] = 1;
>
> Because had I written
>
> c = {b: 1}
>
> I would have ended up with an object where c['b'] == 1; Javascript
does
> not resolve the "expression", 'cause it isn't one.
>
> (That said, certain reserved words like "class" and such do have to
be
> quoted, which means the safe bet is to quote them all, which leads to
> Javascript objects that look identical to Python dicts. But
> 
> {1+2: "moo"}
> 
> will end up different in each language.}

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to