意外的标记 -
Unexpected token -
我有以下工作正常:
var varName= {
variable_one: 'short_name',
variable_two: 'long_name',
variable_three: 'long_name',
variable_four: 'short_name',
variable_five: 'long_name',
variable_six: 'short_name'
};
但如果我变成:
var varName= {
variable-one: 'short_name',
variable-two: 'long_name',
variable-three: 'long_name',
variable-four: 'short_name',
variable-five: 'long_name',
variable-six: 'short_name'
};
它显示错误:
Unexpected token -
那么问题是:它如何转义“-”,因为我需要带有“-”而不是“_”的名称
我试着输入 ''' 或 '"' 但没用 T_T
谢谢
-
是 subtraction operator, you can't use it in an identifier. Use a string (with quote marks around it) for the property name instead. (Object literal syntax 接受 属性 名称的字符串或标识符)。
这应该适合你
let varName= {
'variable-one': 'short_name',
'variable-two': 'long_name',
'variable-three': 'long_name',
'variable-four': 'short_name',
'variable-five': 'long_name',
'variable-six': 'short_name'
};
JavaScript 将 - 作为文字减号。
JavaScript 试图将 -
解释为减号。您也应该能够将名称放入引号中:
var varName= {
'variable-one': 'short_name',
'variable-two': 'long_name',
'variable-three': 'long_name',
'variable-four': 'short_name',
'variable-five': 'long_name',
'variable-six': 'short_name'
};
我有以下工作正常:
var varName= {
variable_one: 'short_name',
variable_two: 'long_name',
variable_three: 'long_name',
variable_four: 'short_name',
variable_five: 'long_name',
variable_six: 'short_name'
};
但如果我变成:
var varName= {
variable-one: 'short_name',
variable-two: 'long_name',
variable-three: 'long_name',
variable-four: 'short_name',
variable-five: 'long_name',
variable-six: 'short_name'
};
它显示错误:
Unexpected token -
那么问题是:它如何转义“-”,因为我需要带有“-”而不是“_”的名称
我试着输入 ''' 或 '"' 但没用 T_T
谢谢
-
是 subtraction operator, you can't use it in an identifier. Use a string (with quote marks around it) for the property name instead. (Object literal syntax 接受 属性 名称的字符串或标识符)。
这应该适合你
let varName= {
'variable-one': 'short_name',
'variable-two': 'long_name',
'variable-three': 'long_name',
'variable-four': 'short_name',
'variable-five': 'long_name',
'variable-six': 'short_name'
};
JavaScript 将 - 作为文字减号。
JavaScript 试图将 -
解释为减号。您也应该能够将名称放入引号中:
var varName= {
'variable-one': 'short_name',
'variable-two': 'long_name',
'variable-three': 'long_name',
'variable-four': 'short_name',
'variable-five': 'long_name',
'variable-six': 'short_name'
};