javascript函数的uncurrying 反柯里化

14118次浏览

什么是反柯里化?

前面我们讲了js函数的柯里化,今天我们再来说说反柯里化。反柯里化,字面意思的柯里化的反义词,对立面。没错,反柯里化的作用在与扩大函数的适用性,使本来作为特定对象所拥有的功能的函数可以被任意对象所用。更通俗的解释说反柯里化是 函数的借用,是函数能够接受处理其他对象,通过借用泛化、扩大了函数的使用范围。只说可能有点迷糊,我们来看看例子:

我们给函数增加一个反柯里化方法!

Function.prototype.unCurrying= function() {
    var that = this;
    return function() {
        return Function.prototype.call.apply(that, arguments);
    }
}

通过上面函数反柯里化,我们让对象拥有数组push的方法!

var push = Array.prototype.push.unCurrying(),
obj = {};
push(obj, 'first', 'two');
console.log(obj);
/*obj {
    0 : "first",
    1 : "two"
}*/

上面就是简单的反柯里化!

通用反柯里化函数

上面例子中把uncurrying写进了prototype,这不太好,我们其实可以把 uncurrying 单独封装成一个函数;

var uncurrying= function (fn) {
    return function () {
        var args=[].slice.call(arguments,1);
        return fn.apply(arguments[0],args);        
    }    
};

通过这个反柯里化函数,我们将String.prototype.split进行改造,形成一个split()方法。

var test="a,b,c";
console.log(test.split(","));//[ 'a', 'b', 'c' ]

var split=uncurrying(String.prototype.split);  
console.log(split(test,','));        //[ 'a', 'b', 'c' ]

再看一个函数push的例子

var haorooms = {};
console.log(haorooms.push);                          // undefined
var pushUncurrying = uncurrying(Array.prototype.push);
haorooms.push = function (obj) {
    pushUncurrying(this,obj);
};
haorooms.push('first');
console.log(haorooms.length);                        // 1
console.log(haorooms[0]);                            // first
console.log(haorooms.hasOwnProperty('length'));  // true

同理,我们将数组的indexOf进行反柯里化!

var indexof=uncurrying(Array.prototype.indexOf);
haorooms.indexOf = function (obj) {
    return indexof(this,obj);
};
haorooms.push("second");
console.log(haorooms.indexOf('first'));              // 0
console.log(haorooms.indexOf('second'));             // 1
console.log(haorooms.indexOf('third'));              // -1

我们还可以 Function.prototype.call/apply 方法 uncurring,例如:

var call= uncurrying(Function.prototype.call);
var fn= function (str) {
    console.log(this.value+str);
};
var obj={value:"欢迎访问"};
call(fn, obj,"haorooms博客!");                       //欢迎访问haorooms博客!

uncurrying 函数进阶

我们看看其他几个版本的反柯里化

var uncurrying= function (fn) {
    return function () {
        var context=[].shift.call(arguments);
        return fn.apply(context,arguments);
    }
};

也可以这么写:

var uncurrying= function (fn) {
    return function () {        
        return Function.prototype.call.apply(fn,arguments);
    }
};

还可以这么写

var uncurrying=Function.prototype.bind.bind(Function.prototype.call);

看到这里是不是有点晕!总之,越简单,越是难理解!越能体现javascript的技巧之处!

javascript函数的 反柯里化,就先写到这里!

Tags: js反柯里化

相关文章: