Forum Moderators: open

Message Too Old, No Replies

Valid key names for Associative Object Arrays

         

coolcat

9:16 pm on Feb 17, 2005 (gmt 0)



Does JavaScript have the same restrictions for key names as for variable names? I'm curious because I've seen a discrepancy between IE and Netscape, specifically in multidimensional arrays where key names may also be values from a parent index.

Is the recommendation to stick to valid variable names and are there any docs that explain this?

Thanks.

Bernard Marx

11:17 pm on Feb 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you're using dot syntax, then the rules are the same as for variables.
But if you use square bracket syntax, or make your object with a literal using quoted names, then you can use any string you like (or even an empty string).

hello = new Object()
// hello.123a //XXX
hello['123a'] = 1;
hello['if'] = 2;
hello['kippers & custard']

// alternatively

hello =
{
'123a': 1,
'if': 2,
'kippers & custard': 3
}

Is the recommendation to stick to valid variable names?

No official one, but it's easier to write:

myObject.doodah

than

myObject['doo dah']

are there any docs that explain this?

Voila

[jibbering.com ]

I've seen a discrepancy between IE and Netscape

Interesting. Can you give us more?