Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   Accessing Numeric Fields in Objects with Dot Notation (http://www.velocityreviews.com/forums/t933133-accessing-numeric-fields-in-objects-with-dot-notation.html)

Debajit Adhikary 10-08-2007 03:11 AM

Accessing Numeric Fields in Objects with Dot Notation
 
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)


Lee 10-08-2007 04:20 AM

Re: Accessing Numeric Fields in Objects with Dot Notation
 
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


--


Thomas 'PointedEars' Lahn 10-08-2007 12:55 PM

Re: Accessing Numeric Fields in Objects with Dot Notation
 
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


All times are GMT. The time now is 12:24 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.