【发布时间】:2018-01-24 07:28:46
【问题描述】:
对于看似正常的代码,我收到了一些非常奇怪的语法错误 [Uncaught SyntaxError: Unexpected identifier]。
如果我在 else 块的第一行之后写了任何东西,就会出错..
if (classlist.length == 0) {
lastClickCatID = -1;
}
else {
lastClickCatID = +classlist.value.split('-')[1];
// **Getting error if I write anything here, even for comments like this**
}
如果我在评论中只使用两个词(它们之间有空格),则会出现错误。当然,任何 JS 语句都会出错,甚至是基本的控制台日志。
此外,以下代码中的 console.log 行出现错误,已被注释掉(第 4 行:console.log-“UNDO”):
// Push this data into the undo array
undo.push([lastClickDivID, lastClickCol, lastClickRow, lastClickCatID, nextClickCatID]);
//console.log("UNDO", undo[0][0], undo[0][1], undo[0][2], undo[0][3], undo[0][4]); // *Getting error if I include this line*
console.log(undo.pop());
无论有无评论标签都会出现错误。但是,如果我删除整行,它就可以正常工作。
我还有一行:
nextClickCatID = +id2;
在执行变量的 console.log 时再次出现错误。但如果我删除“+”并只使用下一个“nextClickCatID = id2;”,它就可以正常工作'。
在这个函数中还会出现许多其他类似的奇怪错误(如果我包含它们会变得太长)。知道为什么我会在看似正常的代码中遇到这样的错误吗?
【问题讨论】:
-
您能否向我们展示一个更完整的代码示例 - 或者理想情况下是您问题中 sn-p 错误的工作示例
-
为什么变量前面有一个+号?
-
@N. Ivanov - + 符号将它们解析为 int。即使我完全删除它或使用 parseInt,我也会遇到一些错误。
-
@RoryMcCrossan 当然,我将在此处的评论部分上传一个代码 sn-p,并在 2 分钟内提供更多详细信息..
标签: javascript jquery syntax-error