I am trying to get a recursion method to work in a class context. Within my class I have the following method:
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;
}
However, when I use this method (which worked before when I just used it as function countChildren = ...
) it can't... find (?) itself: Cannot read property 'countChildren' of undefined
at the recursion.
Does anyone have any ideas?