Dav*_*ery 12 javascript recursion class
我试图让一个递归方法在类上下文中工作.在我的课堂上,我有以下方法:
countChildren(n, levelWidth, level) {
if (n.children && n.children.length > 0) {
if (levelWidth.length <= level + 1) {
levelWidth.push(0);
}
levelWidth[level + 1] += n.children.length;
n.children.forEach(function (n) {
this.countChildren(n, levelWidth, level+1);
});
}
// Return largest openend width
return levelWidth;
}
Run Code Online (Sandbox Code Playgroud)
但是,当我使用这个方法(之前我刚刚使用它时工作function countChildren = ...
)时,它不能......找到(?)本身:Cannot read property 'countChildren' of undefined
在递归时.
有没有人有任何想法?
尝试在构造函数中绑定方法.
此外,通过使用箭头函数,您forEach
可以保持类的范围this
.
export class MyClass {
constructor(){
this.countChildren = this.countChildren.bind(this);
}
countChildren(n, levelWidth, level){ ... }
countChildren(n, levelWidth, level) {
if (n.children && n.children.length > 0) {
if (levelWidth.length <= level + 1) {
levelWidth.push(0);
}
levelWidth[level + 1] += n.children.length;
n.children.forEach( n => { // arrow function do not need to rebind this
this.countChildren(n, levelWidth, level+1);
});
}
// Return largest openend width
return levelWidth;
}
}
Run Code Online (Sandbox Code Playgroud)
问题出现了,因为在你的循环中,this
重新定义到内部函数范围.
countChildren(n, levelWidth, level) {
var self = this; // Get a reference to your object.
if (n.children && n.children.length > 0) {
if (levelWidth.length <= level + 1) {
levelWidth.push(0);
}
levelWidth[level + 1] += n.children.length;
n.children.forEach(function (n) {
// Use "self" instead of "this" to avoid the change in scope.
self.countChildren(n, levelWidth, level+1);
});
}
// Return largest openend width
return levelWidth;
}
Run Code Online (Sandbox Code Playgroud)