Vincent Tang
Vincent Tang

Reputation: 4170

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

The error I'm getting is

Untaught Syntaxerror: Unexpected Token New

When I use the word "new" a second time within the same object array.

(e.g. If I deleted new Course("B title", "B instructor", 1, true, 123456) line, the code works fine

What am I doing wrong here?

Upvotes: 0

Views: 925

Answers (1)

Damien
Damien

Reputation: 1600

You missed the comma , in your array. Fix it. It should be as shown below.

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

Upvotes: 1

Related Questions