坚持 OOP Class
Stuck with OOP Class
您好,我正在尝试 运行 OOP Class 框架中的此代码,但是存在“无法读取未定义的属性(读取 '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)
看了3个小时,终于明白为什么了。
- const{tab} = 这个。解构 return 是对我的引用,因此我无法重新分配制表符的值(在这种情况下显然应该一直是 re-assigned)。因此,请改用 this.tabs。
- 没有本质上未定义的“return”。
- 即使使用 return 我尝试了 return this.tabs 但仍然未定义。原因是我正在链接方法,所以它应该是“return this”,因为这会返回我可以应用下一个方法的对象。
您好,我正在尝试 运行 OOP Class 框架中的此代码,但是存在“无法读取未定义的属性(读取 '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)
看了3个小时,终于明白为什么了。
- const{tab} = 这个。解构 return 是对我的引用,因此我无法重新分配制表符的值(在这种情况下显然应该一直是 re-assigned)。因此,请改用 this.tabs。
- 没有本质上未定义的“return”。
- 即使使用 return 我尝试了 return this.tabs 但仍然未定义。原因是我正在链接方法,所以它应该是“return this”,因为这会返回我可以应用下一个方法的对象。