简体   繁体   English

找不到未捕获的SyntaxError的原因:数组上的无效或意外令牌

[英]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). 另一方面,似乎您可能正在尝试创建一个关联数组(其中,字符串stickstickDamage变量中的数据相关联)。 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.

相关问题 未捕获到的SyntaxError:意外令牌-找不到不可见的字符 - Uncaught SyntaxError: Unexpected token - Can't find invisible character 未捕获的SyntaxError:意外令牌:无法解析 - Uncaught SyntaxError: Unexpected token : Can't resolve 未捕获的SyntaxError:无效或意外的令牌 - Uncaught SyntaxError: Invalid or unexpected token 错误……未捕获的语法错误:无效或意外的令牌……使用 JavaScript - error… Uncaught SyntaxError: Invalid or unexpected token… with JavaScript 未捕获的SyntaxError:无效或意外的令牌弹簧启动 - Uncaught SyntaxError: Invalid or unexpected token spring boot 如何修复未捕获的语法错误:无效或意外的令牌 - how to fix Uncaught SyntaxError: Invalid or unexpected token 未捕获的语法错误:thymeleaf 模板中的令牌无效或意外 - Uncaught SyntaxError: Invalid or unexpected token in thymeleaf template 未捕获的语法错误:无效或意外的令牌(Chrome 扩展) - Uncaught SyntaxError: Invalid or unexpected token (Chrome Extension) 未捕获的语法错误:脚本中的令牌无效或意外 - Uncaught SyntaxError: Invalid or unexpected token in script 未捕获的SyntaxError:具有Scala的javascript中的无效或意外令牌 - Uncaught SyntaxError: Invalid or unexpected token in javascript with scala
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM