具有静态递归方法,在这里我需要有一个应该声明的计数器。
请参见下面的代码片段,我在这里做什么错
一种静态方法提取数据,另一种静态方法将数据格式化为正确的格式
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
我如何在外部声明一个可以用于 recursiveFunction
的变量任何帮助表示赞赏
答案 0 :(得分:2)
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