您好,我正在嘗試在 OOP 類框架中運行此代碼,但是出現“無法讀取未定義的屬性(讀取 'join')的錯誤。
這是為什么?
class Window {
constructor(tabs) {
this.tabs = tabs;
}
join(otherWindow) {
const {tabs} = this;
tabs = tabs.concat(otherWindow.tabs)
}
tabOpen() {
const {tabs} = this;
tabs.push('new tab')
}
tabClose(index) {
const {tabs} = this;
const tabsBeforeIndex = tabs.slice(0, index)
const tabsAfterIndex = tabs.slice(index 1)
tabs = tabsBeforeIndex.concat(tabsAfterIndex)
}
}
const workWindow = new Window(['GMail', 'Inbox', 'Work mail', 'Docs', 'freeCodeCamp'])
const socialWindow = new Window(['FB', 'Gitter', 'Reddit', 'Twitter', 'Medium']);
const videoWindow = new Window(['Netflix', 'YouTube', 'Vimeo', 'Vine']);
const finalTabs = socialWindow.tabOpen().join(videoWindow.tabClose(2)).join(workWindow.tabClose(1).tabOpen());
console.log(finalTabs)
uj5u.com熱心網友回復:
看了3個小時,終于明白為什么了。
- 常量 {標簽} = 這個。解構將參考回傳給我,因此我無法重新分配選項卡的值(在這種情況下顯然應該一直重新分配)。因此,請改用 this.tabs。
- 沒有“回報”,所以本質上是未定義的。
- 即使回傳我嘗試回傳 this.tabs 并且仍然未定義。原因是我正在鏈接方法,所以它應該是“回傳這個”,因為這給了我可以應用下一個方法的物件。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/437078.html
標籤:javascript 班级 哎呀
下一篇:VS2022快速添加類
