【问题标题】:Javascript error Unexpected token new for an object arrayJavascript错误对象数组的意外令牌新
【发布时间】:2017-06-19 03:23:51
【问题描述】:
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 Course("B title", "B instructor", 1, true, 123456) 行,代码可以正常工作

我在这里做错了什么?

【问题讨论】:

  • 我认为你在数组中漏掉了一个逗号
  • 投票结束,这只是一个简单的错字。

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


【解决方案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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-12
    • 1970-01-01
    • 2015-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-12
    • 1970-01-01
    相关资源
    最近更新 更多