javascript 语法错误:JS 中的意外 EOF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26325530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 05:48:50  来源:igfitidea点击:

SyntaxError: Unexpected EOF in JS

javascript

提问by Geronimo

I have the following script:

我有以下脚本:

function getMoods(nb) {
    var index;
    var a = ["Banana", "Coconut", "Peach", "Apple", ...];
    for (index=0; index<nb; ++index) {
        alert('a');
        if(index==1 || index==5 || index==9 || index==13) { moods += '<div class="col-xs-4">'; }
            moods += '
                <div class="checkbox">
                    <label for="'+a[index]+'">
                        <input type="checkbox" id="'+a[index]+'" class="moods"> '+a[index]+'
                    </label>
                </div>';
        if(index==4 || index==8 || index==12) { moods += '</div> '; }
    }
    $("#moods-area").html(moods);
}

I do not understand why I have the following error:

我不明白为什么会出现以下错误:

SyntaxError: Unexpected EOF

Could you please help me ?

请你帮助我好吗 ?

回答by Oriol

There are two problems:

有两个问题:

  1. A wrong use of the spread operator ...:

    ["Banana", "Coconut", "Peach", "Apple", ...];
    

    This throws SyntaxError: expected expression, got ']', because after the spread operator there isn't any iterable object.

  2. JavaScript doesn't support multiline strings.

    You can use some alternatives:

    • Concatenate multiple strings

      moods += 
        '<div class="checkbox">'
          +'<label for="'+a[index]+'">'
            +'<input type="checkbox" id="'+a[index]+'" class="moods"> '+a[index]
          +'</label>'
        +'</div>';
      
    • Use \at the end of each line to continue the string at the next one

      moods += '\
        <div class="checkbox">\
          <label for="'+a[index]+'">\
            <input type="checkbox" id="'+a[index]+'" class="moods"> '+a[index]+'\
          </label>\
        </div>';
      
    • Join an array of strings:

      moods += [
        '<div class="checkbox">',
          '<label for="'+a[index]+'">',
            '<input type="checkbox" id="'+a[index]+'" class="moods"> '+a[index],
          '</label>',
        '</div>'].join('');
      
  1. 错误使用扩展运算符...

    ["Banana", "Coconut", "Peach", "Apple", ...];
    

    这会抛出SyntaxError: expected expression, got ']',因为在展开运算符之后没有任何可迭代对象。

  2. JavaScript 不支持多行字符串。

    您可以使用一些替代方法:

    • 连接多个字符串

      moods += 
        '<div class="checkbox">'
          +'<label for="'+a[index]+'">'
            +'<input type="checkbox" id="'+a[index]+'" class="moods"> '+a[index]
          +'</label>'
        +'</div>';
      
    • \在每一行的末尾使用以在下一行继续字符串

      moods += '\
        <div class="checkbox">\
          <label for="'+a[index]+'">\
            <input type="checkbox" id="'+a[index]+'" class="moods"> '+a[index]+'\
          </label>\
        </div>';
      
    • 加入一个字符串数组:

      moods += [
        '<div class="checkbox">',
          '<label for="'+a[index]+'">',
            '<input type="checkbox" id="'+a[index]+'" class="moods"> '+a[index],
          '</label>',
        '</div>'].join('');