Accessing Numeric Fields in Objects with Dot Notation

  • Thread starter Debajit Adhikary
  • Start date
D

Debajit Adhikary

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)
 
L

Lee

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


--
 
T

Thomas 'PointedEars' Lahn

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

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top