SyntaxError:JS中的意外EOF [英] SyntaxError: Unexpected EOF in JS

查看:1224
本文介绍了SyntaxError:JS中的意外EOF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下脚本:

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 ?

推荐答案

有两个问题:


  1. 错误地使用传播运算符 ...

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

抛出语法错误:期望表达式,得到']',因为在扩展运算符之后没有任何可迭代对象。

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

JavaScript不支持多行字符串。

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('');
    


  • 这篇关于SyntaxError:JS中的意外EOF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆