Time to use our class. We start the same way we'd create a variable, but then add the new
keyword followed Book()
.
Here we'll assign new Book()
to the book1
variable.
class Book {
constructor(author, title) {
this.author = author;
this.title = title;
}
}
var book1 = new Book();
There we have it! We can use classes to easily create as many books as we want now, with way less code.
class Book {
constructor(author, title) {
this.author = author;
this.title = title;
}
}
var book1 = new Book("Leo Tolstoy", "War and Peace");
var book2 = new Book("Dr. Seuss", "Oh, the Places You'll Go!");
console.log(book1);
console.log(book2);