简体   繁体   English

在es6类内创建由静态方法递归使用的计数器变量

[英]creating counter variable inside es6 class used by static methods recursively

having a static recursive method , where i need to have a counter that should be declared. 有一个静态的递归方法,在这里我需要有一个应该声明的计数器。

please see the below snippet what am i doing any wrong here 请查看下面的代码片段我在这里做什么错

one static method extracts data and another static method formats the data into a proper format 一种静态方法提取数据,另一种静态方法将数据格式化为适当的格式

class Method(){
  constructor(payload){
    this.data = Method.extractData(payload)
  }

  static recursiveFunction(list, object ={}, methods){
    // recursive logic at a point 
    if(condtionNotSatisfied){
      this.recursiveFunction([value])
    }else{
      // dont call recursive function
      console.log(this.counter) // giving some times 8, 24
      this.counter = this.counter + 1
    }
  }

  static extractData(payload){
    return this.recursiveFunction(payload.list, {}, payload.methods)
  }

  Method.counter = -1;

}

module.exports = Method

how can i declare a varibale outside which can be used recursiveFunction 我如何在外面声明可以使用递归函数的变量

Any help appreciated 任何帮助表示赞赏

class Method(){
  constructor(payload){
    this.data = Method.extractData(payload)
  }

  static recursiveFunction(list, object ={}, methods){
    if (Method.counter === undefined){
       Method.counter = 0; //initialize here
    }
    // recursive logic at a point 
    if(condtionNotSatisfied){
      this.recursiveFunction([value])
    }else{
      // dont call recursive function
      console.log(Method.counter) // giving some times 8, 24
      Method.counter = Method.counter + 1
    }
  }

  static extractData(payload){
    return this.recursiveFunction(payload.list, {}, payload.methods)
  }

  Method.counter = -1;

}

module.exports = Method

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM