我正在做一个图书馆。
let myLibrary = []; //to store my Book Objects
function Book(title, author, onPage, totalPages) { //constructor function
this.title = title;
this.author = author;
this.onPage = onPage;
this.totalPages = totalPages;
this.info = function() {
return this;
}
}
现在我创建了一个函数,它将生成一个 Book 对象并将用户输入作为属性值:
function addBookToLibrary() {
let a = document.getElementById('bookTitle').value;
let b = document.getElementById('authorName').value;
let c = document.getElementById('onPage').value;
let d = document.getElementById('numOfPages').value;
myLibrary.push(new Book(a, b, c, d));
}
我创建了一个函数,它将循环遍历myLibrary数组并根据属性值对它们进行排序。例如:
function showBooks() {
for (i = 0; i <= myLibrary.length; i++) {
if (Number(myLibrary[i].onPage) < Number(myLibrary[i].totalPages)
let newLi = document.createElement('li');
newLi.innterText = myLibrary[i].title + ' by ' + myLibrary[i].author;
inProgressBooks.prepend(newLi);
}
}
}
现在我将加载我的页面并使用 addBookToLibrary 函数添加一个对象。现在 myLibrary[0].onPage 返回一个由 addBookToLibrary 从输入中获取的数字。那么为什么函数 showBooks 会失败呢?它说它无法读取未定义的 onPage 属性。
斯蒂芬大帝
慕的地10843
随时随地看视频慕课网APP
相关分类