繁体   English   中英

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

[英]Javascript error Unexpected token new for an object array

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:意外令牌新

当我在同一对象数组中第二次使用单词“ new”时。

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

我在这里做错了什么?

你错过了逗号,你的阵列英寸 修理它。 应该如下图所示。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM