I coded something the other day and used numbers as property names in a literal object, e.g.
Code:
var myObj = {
o: "prop1",
1: "prop2",
etc...
}
It didn't throw any errors or warnings (and I have strict warnings enabled in firebug) so I'm guessing its valid code, but it just felt wrong somehow...
Can anyone confirm whether the obj definition above is correct/valid/ok..?
_ $ A-Z a-z 0-9 are all valid variable names in JavaScript. And if it works, yes, it's valid.
EDIT: You don't ever need to use a string as a property name in JSON
Code:
var myobj = {a:function(){alert("");}};
myobj.a();
is valid too. The only time you have to use a string is when using variables with spaces (never do that even though I think it works), or when accessing a property like this
Code:
myobj["a"]();
Last edited by Declan1991; 01-08-2009 at 02:46 PM.
Great wit and madness are near allied, and fine a line their bounds divide.
Bookmarks