5

I noticed that if I try to create an object with a key name that leads with a numeric value, an error is thrown (which goes along with JavaScript naming outlined here: What characters are valid for JavaScript variable names?). However, I noticed that I can still add such a variable name dynamically if I do so

Fails:

object.1foo = "bar";

Fails:

object = {
  1foo: "bar"
}

Succeeds:

object["1foo"] = bar;

Why is that?

Community
  • 1
  • 1
stinkycheeseman
  • 43,437
  • 7
  • 30
  • 49
  • They are semantically different constructs that happen to mean the same thing to the runtime environment. – asawyer Dec 22 '11 at 13:54

4 Answers4

3

When you do object["1foo"] you are actually escaping the name of the property, so that's why it works.

The other too fail because 1foo isn't escaped, it's in the same form.

If you want to access object members with standard dot notation (eg. object.property) you aren't allowed to use any special characters besides _ or $; Aside from that, the name cannot start with a number. For all other cases you are forced to use square brackets and quotation marks to access object elements.

alessioalex
  • 62,577
  • 16
  • 155
  • 122
2

When accessing object["1foo"] you are not creating an identifier but a key to look up a value inside your object.

Identifiers cannot begin with a digit, keys don't have any such specifications (of kind of "obvious" reasons.

Identifiers are not values, keys (in the sense described in this post) are not identifiers.

When doing {"1foo": 123} you are not making 1foo a proper identifier, it's a rvalue variable of type string.


According to the ECMA standard

7.6 Identifier Names and Identifiers

Identifier ::

  • IdentifierName but not ReservedWord

IdentifierName ::

  • IdentifierStart
  • IdentifierName, IdentifierPart

IdentifierStart ::

  • UnicodeLetter
  • $
  • _
  • \ UnicodeEscapeSequence

IdentifierPart ::

  • IdentifierStart
  • UnicodeCombiningMark
  • UnicodeDigit
  • UnicodeConnectorPunctuation
  • <ZWNJ>
  • <ZWJ>

...

11.1.5 Object Initializer

PropertyName [can be any of the following]:

  • IdentifierName
  • StringLiteral
  • NumericLiteral
Community
  • 1
  • 1
Filip Roséen - refp
  • 62,493
  • 20
  • 150
  • 196
2

How identifiers are formed is a syntactic rule, that is, it only applies when the source of your program is being parsed. At run time, object fields' names are just arbitrary strings, and the engine does not care if these strings are "valid" from the parser's point of view:

obj = {}

obj["foo"] - ok
obj["1xx"] - ok
obj["{{{"] - ok
obj["   "] - ok
georg
  • 211,518
  • 52
  • 313
  • 390
1

You should not mix valid variable names and valid property names. In your case you're dealing with object properties and according to "Object Initialiser" section of ECMAScript specification object property name should be valid identifier, or valid string, or valid number:

ObjectLiteral :
    {}
    { PropertyNameAndValueList }

PropertyNameAndValueList :
    PropertyName : AssignmentExpression
    PropertyNameAndValueList , PropertyName : AssignmentExpression

PropertyName :
    Identifier
    StringLiteral
    NumericLiteral 

As 1foo is not valid identifier (see "Identifiers" section) you can't use it directly to define property. But (besides ways you described in the question) you can do it like that using string property name:

object = { '1foo': 'bar' }
KL-7
  • 46,000
  • 9
  • 87
  • 74