Create a Book class with private fields title, author, and year. Provide getters for each field and a setter for the year field that raises a RangeError if year is before 1900.
class Book {
// your code here
}
let book = new Book('The Great Gatsby', 'F. Scott Fitzgerald', 1925);
console.log(book.title); // The Great Gatsby
console.log(book.author); // F. Scott Fitzgerald
console.log(book.year); // 1925
book.year = 1932; // Changing year
console.log(book.year); // 1932
try {
book.year = 1825;
} catch (e) {
console.log(e); // RangeError: Invalid year
}
try {
let book2 = new Book('A Tale of Two Cities', 'Charles Dickens', 1859);
} catch (e) {
console.log(e); // RangeError: Invalid year
}