Referencing Object Properties with Numeric Names
Despite the documentation's suggestion that object literal property names can be integers, accessing these properties using the dot syntax (e.g., me.123) fails.
Alternative Syntax: Array-Style Access
To access an object property with an integer name, you must use the array-style syntax:
This syntax behaves as if the property were an element of an array, with 123 being the index.
String Notation
Alternatively, you can use string notation to access the property:
This approach encloses the integer in double quotes, treating it as a string.
Example:
In your example, you can access the property with integer name 123 using:
console.log(me[123]); // Output: 26
The above is the detailed content of How to Access Object Properties with Numeric Names in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!