curry柯里化

Function.prototype.method = function(name,func){
            if(!this.prototype[name]){
                this.prototype[name] = func;
            }
            return this;
        }
        Function.method('curry', function(){
            var slice = Array.prototype.slice,
                args = slice.apply(arguments), 
                that = this;
            return function(){
                return that.apply(null,args.concat(slice.apply(arguments)));
            }

        })
        var add = function(a, b){
            if(typeof a !== 'number' || typeof b !== 'number'){
                throw{
                    name:'TypeError',
                    message:'add needs numbers'
                }
            }
            return a+b;
        }
        var add1 = add.curry(1);
        document.write(add1(6));

 

posted on 2017-07-04 10:26  yanyiyi  阅读(108)  评论(0编辑  收藏  举报