我正在尝试制作一个 BookList 应用程序(基于 Traversy Media 教程之一),它将您输入的一本书添加到网站上可见的列表和本地存储中,因此在您刷新网站后,将显示您之前添加的书籍。但是我不能创建基于 Book 类的对象。发生事件后submit,输入中的所有值都分配给title, author,isbn变量。然后我使用这些变量来创建一个基于 Books 类的新书对象。Evertyhing 看起来不错,这本书正在添加到表格中,但是当我在book控制台中输入时,它说这本书没有定义。为什么会这样?这是我的 JavaScript 代码的一部分:
//Creating a Book Class
class Book {
constructor(title, author, isbn) {
this.title = title;
this.author = author;
this.isbn = isbn;
}
}
// Using submit button
document.addEventListener('submit', () => {
title = document.querySelector('#title').value;
author = document.querySelector('#author').value;
isbn = document.querySelector('#isbn').value;
const book = new Book(title, author, isbn); // Creating an object
UI.addBookToTable(title, author, isbn); // Adding book to visible list
})