Javascript recursion within a class

后端 未结 5 956
轻奢々
轻奢々 2020-12-31 09:28

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) {
            


        
5条回答
  •  借酒劲吻你
    2020-12-31 10:08

    The this inside the foreach than the this in the class. In your case, this refers to the current element being iterated.

    you need to bind the scope.

    n.children.forEach(function (n) {
       this.countChildren(n, levelWidth, level+1);
    }.bind(this));   
    

提交回复
热议问题