Javascript错误对象数组的新意外令牌

时间:2017-06-19 03:23:51

标签: javascript arrays object error-handling syntax-error

function Course(title,instructor,level,published,views){
    this.title = title;
    this.instructor = instructor;
    this.level = level;
    this.published = published;
    this.updateViews = function() {
        return ++this.views;
    }
}

var courses = [
    new Course("A title", "A instructor", 1, true, 0)
    new Course("B title", "B instructor", 1, true, 123456)
];


console.log(courses);

我得到的错误是

  

Untaught Syntaxerror:Unexpected Token New

当我使用" new"第二次在同一个对象数组中。

(例如,如果我删除了new Course("B title", "B instructor", 1, true, 123456)行,则代码正常

我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

您错过了数组中的逗号,。修理它。它应该如下所示。

function Course(title,instructor,level,published,views){
    this.title = title;
    this.instructor = instructor;
    this.level = level;
    this.published = published;
    this.updateViews = function() {
        return ++this.views;
    }
}

var courses = [
    new Course("A title", "A instructor", 1, true, 0),
    new Course("B title", "B instructor", 1, true, 123456)
];


console.log(courses);