javascript - 如何使用在其类上创建的方法将从类创建的对象添加到数组
问题描述
我创建了一个 Book 类,其中包含一个在单击按钮时将单个对象添加到数组的方法。但是当我单击它时,添加的是来自 DOM 的按钮本身。
这是代码:
class Book {
constructor(title, author, year, poster){
this.title = title;
this.author = author;
this.year = year
this.poster = poster
}
addToFavourite(){
favouriteBooks.push(this)
}
}
const favouriteBooks = []
const booksList = [ new Book('you don\'t know js', ' Kyle Simpson', 2014, 'https://images-na.ssl-images-amazon.com/images/I/41T5H8u7fUL._SX331_BO1,204,203,200_.jpg'),
new Book('javascript the definitive guide', 'David Flanagan', 1996, 'https://images-na.ssl-images-amazon.com/images/I/51wijnc-Y8L._SX379_BO1,204,203,200_.jpg'),
new Book('Eloquent JavaScript', 'Marijn Haverbeke', 2011, 'https://eloquentjavascript.net/img/cover.jpg')
]
const renderBooks = (booksToRender)=>{
booksToRender.forEach(book =>{
//elements to be created
const bookContainer = document.createElement('article')
const posterContainer = document.createElement('div')
const poster = document.createElement('img')
const bookTitle = document.createElement('h2')
const bookYear = document.createElement('p')
const bookAuthor = document.createElement('h3')
const addToFavouriteBtn = document.createElement('button')
//elements setup
addToFavouriteBtn.addEventListener('click', book.addToFavourite)
poster.src = book.poster
bookTitle.textContent = book.title
bookYear.textContent = book.year
bookAuthor.textContent = book.author
addToFavouriteBtn.textContent = 'Add to favourite'
//elements render
posterContainer.append(poster)
bookContainer.append(posterContainer, bookTitle, bookYear, bookAuthor, addToFavouriteBtn)
document.querySelector('main').append(bookContainer)
})
}
renderBooks(booksList)
解决方案
那是因为this
点击事件触发时引用的是按钮而不是书籍,您需要将书籍绑定到方法,以便addToFavouriteBtn.addEventListener('click', book.addToFavourite)
使其addToFavouriteBtn.addEventListener('click', book.addToFavourite.bind(book))
按预期工作
class Book {
constructor(title, author, year, poster){
this.title = title;
this.author = author;
this.year = year
this.poster = poster
}
addToFavourite(){
favouriteBooks.push(this)
// just demonstration
console.clear();
console.log("---Favourite Books:---\n", favouriteBooks);
}
}
const favouriteBooks = []
const booksList = [ new Book('you don\'t know js', ' Kyle Simpson', 2014, 'https://images-na.ssl-images-amazon.com/images/I/41T5H8u7fUL._SX331_BO1,204,203,200_.jpg'),
new Book('javascript the definitive guide', 'David Flanagan', 1996, 'https://images-na.ssl-images-amazon.com/images/I/51wijnc-Y8L._SX379_BO1,204,203,200_.jpg'),
new Book('Eloquent JavaScript', 'Marijn Haverbeke', 2011, 'https://eloquentjavascript.net/img/cover.jpg')
]
const renderBooks = (booksToRender)=>{
booksToRender.forEach(book =>{
//elements to be created
const bookContainer = document.createElement('article')
const posterContainer = document.createElement('div')
const poster = document.createElement('img')
const bookTitle = document.createElement('h2')
const bookYear = document.createElement('p')
const bookAuthor = document.createElement('h3')
const addToFavouriteBtn = document.createElement('button')
//elements setup
addToFavouriteBtn.addEventListener('click', book.addToFavourite.bind(book))
poster.src = book.poster
bookTitle.textContent = book.title
bookYear.textContent = book.year
bookAuthor.textContent = book.author
addToFavouriteBtn.textContent = 'Add to favourite'
//elements render
posterContainer.append(poster)
bookContainer.append(posterContainer, bookTitle, bookYear, bookAuthor, addToFavouriteBtn)
document.querySelector('main').append(bookContainer)
})
}
renderBooks(booksList)
<main></main>
推荐阅读
- javascript - 我可以选择一个类型的所有反应组件,而不为每个组件分配一个类吗?
- ajax - 如何使用 jQuery Ajax 访问 Twitter API?
- python - 字节格式的Python列表如何转换为常规列表
- sql - 在 Dynamic Query 中的 SQL Server 中从字符串到日期时间的转换失败
- linux - 在 shell 脚本中迭代地打印特定格式的文件数
- php - 带有 php codeigniter 的 Bootstrap 4 动态导航药丸不起作用
- javascript - 由于 javascript,Selenium 选择了错误的下拉选项
- amazon-web-services - Amazon Athena 中的重复值
- css - 如何用图片替换 WordPress 页面标题?
- sql - SQL Server - 通过唯一键查找行模式组