Count functions calls with JavaScript
问题 For example: I have a lot of functions and use them many times. I need to count calls for each function. What is the best practice to make it? At first i thought i need closures, but I can't implement it in a right way. 回答1: In the simplest case, you can decorate each function with a profiling wrapper: _calls = {} profile = function(fn) { return function() { _calls[fn.name] = (_calls[fn.name] || 0) + 1; return fn.apply(this, arguments); } } function foo() { bar() bar() } function bar() { }