Javascript recursion within a class

后端 未结 5 1003
轻奢々
轻奢々 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:09

    Try using .call() to invoke the function. That way you can specify the context directly.

    Like this:

    this.countChildren.call(this, n, levelWid);th, level+1

    Edit: Noticed my error, what you should really do is bind the anonymous function:

    like this:

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

提交回复
热议问题