[英]Can't find the cause of Uncaught SyntaxError: Invalid or unexpected token on an array
I know there are a lot of questions like this but it seems like a really basic problem so it doesn't look like anyone else has this issue. 我知道有很多这样的问题,但是这似乎是一个非常基本的问题,因此看起来没有其他人遇到这个问题。 the line its referencing is
其引用的行
var stick = [‘stick’, stickDamage];
and it reports uncaught syntax error when it gets there and it can't continue on. 并且报告到达该位置且无法继续运行时出现未捕获的语法错误。 am I writing the array wrong?
我写的数组错了吗?
You are using smart quotes ( ''
) instead of straight quotes ( ''
). 您正在使用智能引号(
''
),而不是直引号( ''
)。
Use a basic text editor that doesn't apply formatting to the text (there are numerous free web editors out there, NotePad++ is a good one) and save your code with UTF-8
encoding. 使用不对文本应用格式设置的基本文本编辑器(那里有许多免费的Web编辑器, NotePad ++是一个不错的选择),然后使用
UTF-8
编码保存代码。
On a different note, it seems like you may be trying to create an associative array (where the string stick
is associated with the data in the stickDamage
variable). 另一方面,似乎您可能正在尝试创建一个关联数组(其中,字符串
stick
与stickDamage
变量中的数据相关联)。 JavaScript doesn't use arrays for this kind of associative data structure. JavaScript不会将数组用于此类关联数据结构。 Instead, it uses objects, that have keys (which are always strings and because of that, you don't even need to put quotes around the key name) and values.
取而代之的是,它使用具有键(始终为字符串,因此甚至不需要在键名周围加上引号)和值的对象。
In that case, you'd want this instead of an array: 在这种情况下,您希望使用它而不是数组:
const stickDamage = 99; const punchDamage = 59; const cutDamage = 79; var player = { stick: stickDamage, punch: punchDamage, cut: cutDamage }; console.log(player.stick, player.punch, player.cut);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.