Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 函数计数调用_Javascript_Function_Closures - Fatal编程技术网

Javascript 函数计数调用

Javascript 函数计数调用,javascript,function,closures,Javascript,Function,Closures,我是JavaScript的初学者,所以请耐心点=) 我正在尝试编写一个计算调用次数的函数。到目前为止,我得到的是一个计数器显式递增的函数: var increment = function () { var i = 0; this.inc = function () {i += 1;}; this.get = function () {return i;}; }; var ob = new increment(); ob.inc(); ob.inc(); alert(o

我是JavaScript的初学者,所以请耐心点=)

我正在尝试编写一个计算调用次数的函数。到目前为止,我得到的是一个计数器显式递增的函数:

var increment = function () {
    var i = 0;
    this.inc = function () {i += 1;};
    this.get = function () {return i;};
};

var ob = new increment();
ob.inc();
ob.inc();
alert(ob.get());

但是我想知道如何只调用
ob(),因此函数可以自动增加对自身的调用。这可能吗?如果可能,如何实现?

将计数器包装到任何函数:

var increment = function() {
    var i = 0;
    return function() { return i += 1; };
};

var ob = increment();
/**
*将计数器包装到函数
*计算函数被调用的次数
*@param{Function}fn要计数的函数
*@param{Number}计数计数器,默认为1
*/
函数addCounterToFn(fn,计数=1){
返回函数(){
fn.apply(空,参数);
返回计数++;
}
}
请参见单行程序选项:

const counter=((count=0)=>()=>count++)()
用法示例:

>计数器()
0
>计数器()
1.
>计数器()
2.
>计数器()
3.
>计数器()
4.
>计数器()
5.
>计数器()
6.

还有新的,它提供了一种编写计数器的简单方法:

function* makeRangeIterator(start = 0, end = 100, step = 1) {
  let iterationCount = 0;
  for (let i = start; i < end; i += step) {
    iterationCount++;
    yield i;
  }
  return iterationCount;
}

const counter = makeRangeIterator();
const nextVal = () => counter.next().value;

console.log("nextVal: ", nextVal()); // 0
console.log("nextVal: ", nextVal()); // 1
console.log("nextVal: ", nextVal()); // 2
console.log("nextVal: ", nextVal()); // 3
function*makeRangeIterator(开始=0,结束=100,步骤=1){
设iterationCount=0;
for(让i=开始;i<结束;i+=步骤){
迭代计数++;
产量一;
}
返回迭代计数;
}
常量计数器=makeRangeIterator();
const nextVal=()=>counter.next().value;
console.log(“nextVal:,nextVal());//0
console.log(“nextVal:,nextVal());//1.
console.log(“nextVal:,nextVal());//2.
console.log(“nextVal:,nextVal());//3.

+1@纳文:努伯似乎是独自一人朝那个方向走去的@谢夫:我说这很好。过了一段时间我才意识到有关闭。但是js是我的第二语言。一个有Alertbox的小优惠:)@naveen:我刚刚同意你的看法一、 我也很惊讶地看到OP,一个自称为初学者的人,试图学习闭包。OP的代码几乎和我第一次尝试学习JS的代码一样。作为noob JS程序员并不意味着成为noob程序员。@Vaibhav:increment函数本身不会增加任何内容。它返回一个函数
ob
保留该功能。
function* makeRangeIterator(start = 0, end = 100, step = 1) {
  let iterationCount = 0;
  for (let i = start; i < end; i += step) {
    iterationCount++;
    yield i;
  }
  return iterationCount;
}

const counter = makeRangeIterator();
const nextVal = () => counter.next().value;

console.log("nextVal: ", nextVal()); // 0
console.log("nextVal: ", nextVal()); // 1
console.log("nextVal: ", nextVal()); // 2
console.log("nextVal: ", nextVal()); // 3