Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Accessing Numeric Fields in Objects with Dot Notation

Reply
Thread Tools

Accessing Numeric Fields in Objects with Dot Notation

 
 
Debajit Adhikary
Guest
Posts: n/a
 
      10-08-2007
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)

 
Reply With Quote
 
 
 
 
Lee
Guest
Posts: n/a
 
      10-08-2007
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/pu...s/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


--

 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      10-08-2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to convert Infix notation to postfix notation Tameem C Programming 454 01-31-2014 06:01 PM
Hungarian Notation Vs. Pascal Notation? Grey Squirrel ASP .Net 6 03-21-2007 09:42 AM
Dynamically resize visible text and append dot dot dot (like gmail subject lines.. ) how to question? jawolter@gmail.com Javascript 6 12-12-2005 04:09 PM
Ordering on Hierarchical Dot Notation dar7yl Java 4 01-23-2005 07:32 PM
Dot notation V Bracket notation Robert Mark Bram Javascript 3 07-05-2003 03:59 AM



Advertisments