【问题标题】:Weird syntax error for just comment lines仅注释行的奇怪语法错误
【发布时间】: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


【解决方案1】:

我已经解决了。当我在 jquery 文档就绪函数中删除 () 和大括号之间的空格时,它奇怪地起作用了。改自...

$(function() {

到:

$(function(){   // with no space between the  parentheses and the curly brackets

我想补充一点——在此之前它使用相同的结构(括号之间有空格)一直运行良好。但是今天突然决定在某个特定的函数中报错。如果有人能澄清为什么会发生这种情况,它仍然是非常相关的。

【讨论】:

    猜你喜欢
    • 2014-04-14
    • 2022-01-12
    • 2015-07-22
    • 2013-09-03
    • 2014-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多