www.webdeveloper.com
Results 1 to 3 of 3

Thread: Quick JS question

  1. #1
    Join Date
    Dec 2006
    Posts
    56

    Quick JS question

    Just a quickie;

    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..?

    Ta

  2. #2
    Join Date
    Aug 2007
    Posts
    3,767
    _ $ 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.

  3. #3
    Join Date
    Dec 2006
    Posts
    56
    cool, thanks for the reply

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles