
代码如下:
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
console.log报Uncaught TypeError: Assignment to constant varia
开发移动端网页如何调用控制台(Eruda、vconsole)
标签: console
上面是“JS如何实现console执行次数”的全面内容,想了解更多关于 js 内容,请继续关注web建站教程。
当前网址:https://m.ipkd.cn/webs_2144.html
声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

CopyDone AI官网:一键生成营销文本、配图与短视频等多类型内容
php检测数组是否为空的三大方法
一款阿里巴巴旗下的智能在线工具,一键高效抠图工具——顽兔AI抠图