力扣之2666.只允许一次函数调用

力扣之2666.只允许一次函数调用_第1张图片

/**
 * @param {Function} fn
 * @return {Function}
 */
var once = function(fn) {
    let on = false
    return function(...args){
        if(on){
            return undefined
        }else{
            on = true
            return fn(...args)
        }
        
    }
};

/**
 * let fn = (a,b,c) => (a + b + c)
 * let onceFn = once(fn)
 *
 * onceFn(1,2,3); // 6
 * onceFn(2,3,6); // returns undefined without calling fn
 */

 力扣之2666.只允许一次函数调用_第2张图片

你可能感兴趣的:(力扣,leetcode,javascript,前端)