函数计数调用 [英] Function count calls
本文介绍了函数计数调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是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(); code>,因此函数可以自动增加对自身的调用。 解决方案
var increment = function(){
var i = 0;
return function(){return i + = 1; };
};
var ob = increment();
I'm a beginner with JavaScript so please be patient =)
I am trying to write a function that counts the number of times it is called. What I have so far is a function with a counter that is incremented explicitly:
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());
But I'm wondering how to call only ob();
, so the function could increment calls made to itself automatically. Is this possible and if so, how?
解决方案 var increment = function() {
var i = 0;
return function() { return i += 1; };
};
var ob = increment();
这篇关于函数计数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文