How to Add Static Methods to a Class in JavaScript

class Book {
    constructor(isbn, title, author, publishedDate) {
        this.isbn = isbn;
        this.title = title;
        this.author = author;
        this.publishedDate = publishedDate;
    }

    static booksEqual(book, book2) {
        if (book instanceof Book && book2 instanceof Book) {
            return (book.isbn.replaceAll('-', '') === book2.isbn.replaceAll('-', ''));
        }
        else {
            return false;
        }

    }
}

const b1 = new Book('897-23-765429', 'JavaScript Programming', new Date());
const b2 = new Book('897-23-765421', 'JavaScript Programming', new Date());
console.log(Book.booksEqual(b1, b2));




Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *