Dynamically Adding A Variable Whose Name Leads With A Numeric Value
Solution 1:
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.
Solution 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
Solution 3:
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
Solution 4:
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 :AssignmentExpressionPropertyNameAndValueList,PropertyName :AssignmentExpressionPropertyName :IdentifierStringLiteralNumericLiteral
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' }
Post a Comment for "Dynamically Adding A Variable Whose Name Leads With A Numeric Value"