Open H246802 opened 5 years ago
function once(fnParam) {
if(Object.prototype.toString.call(fn) !== "[object Function]"){
alert('once函数接受的参数必须是一个函数')
return
}
let flag = true
return function(){
if(flag){
flag = false
fnParam()
}
return
}
}
let log = once(function() {
console.log(1)
})
log() // print 1
log() // nothing or undefined
log() // nothing or undefined
log() // nothing or undefined
写出一个
once
函数,满足以下条件once
函数只接受一个参数fnParam
,这个参数必须是一个函数once
函数返回一个函数fnResult
fnResult
,则执行一次fnParam
fnResult
,也只会执行一次fnParam
示例如下