Accessing Numeric Fields in Objects with Dot Notation

Discussion in 'Javascript' started by Debajit Adhikary, Oct 8, 2007.

  1. Let's say I have the following code:

    var o = {"field1" : "value1", 10 : "value10"};

    Now, if I say o.10
    that does not work with any browser. Why exactly is this an error?

    (in particular, could anyone please refer me to any authoritative
    source that defines how little things like these should work? I
    understand that I could have used o[10] and that would work)
    Debajit Adhikary, Oct 8, 2007
    #1
    1. Advertising

  2. Debajit  Adhikary

    Lee Guest

    Debajit Adhikary said:
    >
    >Let's say I have the following code:
    >
    >var o = {"field1" : "value1", 10 : "value10"};
    >
    >Now, if I say o.10
    >that does not work with any browser. Why exactly is this an error?
    >
    >(in particular, could anyone please refer me to any authoritative
    >source that defines how little things like these should work? I
    >understand that I could have used o[10] and that would work)


    In these specs:
    http://www.ecma-international.org/publications/standards/Ecma-262.htm

    You'll find that an identifier must begin with one of:
    A letter
    An underscore
    A dollar sign
    A Unicode escape sequence


    --
    Lee, Oct 8, 2007
    #2
    1. Advertising

  3. Debajit Adhikary wrote:
    > Let's say I have the following code:
    >
    > var o = {"field1" : "value1", 10 : "value10"};
    >
    > Now, if I say o.10
    > that does not work with any browser. Why exactly is this an error?


    Because `10' is not an identifier (such have to start with a UnicodeLetter,
    `$', `_' or a UnicodeEscapeSequence, see ECMAScript Ed. 3, section 7.6), but
    that an identifier is required for the dot property accessor notation.

    > (in particular, could anyone please refer me to any authoritative
    > source that defines how little things like these should work?


    http://jibbering.com/faq/

    > I understand that I could have used o[10] and that would work)


    Correct. The bracket property accessor notation allows any subscript,
    which is eventually implicitly type-converted to string (ES3, 11.2.1).
    So you could have used o["10"] successfully here, too.


    PointedEars
    --
    Anyone who slaps a 'this page is best viewed with Browser X' label on
    a Web page appears to be yearning for the bad old days, before the Web,
    when you had very little chance of reading a document written on another
    computer, another word processor, or another network. -- Tim Berners-Lee
    Thomas 'PointedEars' Lahn, Oct 8, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dar7yl
    Replies:
    4
    Views:
    560
    dar7yl
    Jan 23, 2005
  2. Grey Squirrel

    Hungarian Notation Vs. Pascal Notation?

    Grey Squirrel, Mar 19, 2007, in forum: ASP .Net
    Replies:
    6
    Views:
    1,270
    Steve C. Orr [MCSD, MVP, CSM, ASP Insider]
    Mar 21, 2007
  3. Tameem
    Replies:
    454
    Views:
    11,567
  4. Robert Mark Bram

    Dot notation V Bracket notation

    Robert Mark Bram, Jul 4, 2003, in forum: Javascript
    Replies:
    3
    Views:
    456
    Robert Mark Bram
    Jul 5, 2003
  5. Replies:
    6
    Views:
    225
    Thomas 'PointedEars' Lahn
    Dec 12, 2005
Loading...

Share This Page