Я программирую библиотеку (в ES6 Javascript) с тремя классами: часы работы, книги, книги. И «основная» библиотека классов (которая состоит из предыдущих классов).
Я получаю следующую ошибку консоли: Uncaught TypeError: Невозможно установить свойство undefined из undefined в книгах Books.set [как книги].
Ошибка в классе Книги в установщике.
Вот код:
'use strict';
//OO library (Classes: Libary, Book, WorkingHours)
class WorkingHours{
constructor(open, close, lunch){
this.open = open;
this.close = close;
this.lunch = lunch;
}
}
class Book {
constructor(title, category, author){
this.title = title;
this.category = category;
this.author = author;
}
}
class Books {
constructor(){
this.books = [];
//let books = new Array();
//let books = [];
var bookAmount = 0;
}
set books(book){
//this.books.push(book);
this.books[this.bookAmount] = book;
this.bookAmount++;
}
}
class Library {
constructor(workingHours, booksIn){
this.workingHours = workingHours;
this.booksIn = booksIn;
}
get workingHours() {
return this.workingHours;
}
get booksIn() {
return this.booksIn;
}
}
var workHour = new WorkingHours(900,1700,false);
var bookColl = new Books();
var newBook = new Book("Mastery", "Real Stories", "Robert Greene");
bookColl.books = newBook;
newBook = new Book("48 Laws of Power", "Slef-teaching", "Robert Greene");
bookColl.books = newBook;
var newLib = new Library(workHour, bookColl);
console.log(newLib);