2024年06月13日 建站教程
代码如下:
var getFunCallTimes = (function() {
// 装饰器,在当前函数执行前先执行另一个函数
function decoratorBefore(fn, beforeFn) {
return function() {
var ret = beforeFn.apply(this, arguments)
// 在前一个函数中判断,不需要执行当前函数
if (ret !== false) fn.apply(this, arguments)
}
}
// 执行次数
var funTimes = {}
// 给fun添加装饰器,fun执行前将进行计数累加
return function(fun, funName) {
// 存储的key值
funName = funName || fun
// 不重复绑定,有则返回
if (funTimes[funName]) return funTimes[funName]
// 绑定
funTimes[funName] = decoratorBefore(fun, function() {
// 计数累加
funTimes[funName].callTimes++
console.log('count', funTimes[funName].callTimes)
})
// 定义函数的值为计数值(初始化)
funTimes[funName].callTimes = 0
return funTimes[funName]
}
})()
function someFunction() {
}
function otherFunction() {
}
someFunction = getFunCallTimes(someFunction, 'someFunction');
someFunction(); // count 1
someFunction(); // count 2
someFunction(); // count 3
someFunction(); // count 4
console.log(someFunction.callTimes); // 4
otherFunction = getFunCallTimes(otherFunction);
otherFunction(); // count 1
console.log(otherFunction.callTimes); // 1
otherFunction(); // count 2
console.log(otherFunction.callTimes); // 2
本文链接:http://so.lmcjl.com/news/6471/