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


我头上有犄角
2025-03-12 05:42:36 (5天前)


有一个静态递归方法,我需要有一个应该声明的计数器。

请看下面的代码片段我在这里做错了什么

一个静态方法提取数据和另一个…

2 条回复
  1. 0# v-star*위위 | 2019-08-31 10-32




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

    2. 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
      }
      }

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

    4. Method.counter = -1;

    5. }

    6. module.exports = Method

    7. </code>

登录 后才能参与评论