var test = "test123"
var test123 ={
"key" + test: 123
}
This code doesn’t work. What is wrong with “key” + test ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Because
"key" + testis an expression and not an identifier nor a string literal nor a number literal, which are the only things that are allowed as the key in an object literal.You have to use the
[]notation after creating the object for such a dynamic key:An identifier is basically the same subset of characters that you can call a variable (letters, numbers,
_and$; may not start with a number), and a string literal is any string enclosed with'or".So, the only types of keys you can use in an object literal are:
Reference: http://es5.github.com/#x11.1.5.