一位 friend 在 Javascript 工作面试中遇到了这个问题:
解释一下这个计数器是如何工作的(换句话说,什么是非最小化版本)
let Counter = (_=1)=>$=>_++
let c1 = Counter()
console.log(c1()) //1
console.log(c1()) //2
最佳答案
这与
相同const Counter = function(counter = 1) {
return function count() {
return counter++;
};
}
所以基本上,当您调用 Counter()
时,它会以 1
作为默认值封装 counter
,并且每次您调用由它返回计数器值并将其增加 1
关于javascript - 为什么 (_=1)=>$=>_++ 是一个计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52768382/