js对函数参数的封装

对函数参数的封装

一个原始函数有n个参数,用wrap对函数进行封装,生成一个新的函数,当给它传入参数数量为n的时候,将执行原始函数,否则不执行

//函数封装
function wrap(func){
    let len=func.length,cache=[]
    return function () {
        cache.push.apply(cache,arguments)
        if(cache.length<len){
            return arguments.callee
        }else{
            return func.apply(null,cache)
        }
    }
}

var func=function (a,b,c) {
    return a+b+c
}

var n=wrap(func)
console.log(n(1)(2)(2))

  

猜你喜欢

转载自www.cnblogs.com/caoke/p/9951023.html
今日推荐