Javascript recursion within a class

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

    the variable this Gets redefined within:

    1. the inner scope of a for loop
    2. within a inline function declariation
    3. within asynchronous function calls.

    I agree with krillgar with the declaration of self. it fixed my problem with an asynchronous call.

    obj.prototype.foo = function (string){
       var self = this;
       if(string){ do something }
       else
       setTimeout(function(){
         self.foo("string");
         }, 5000);
    }
    

提交回复
热议问题