javascript函数的uncurrying 反柯里化

什么是反柯里化?

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

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

javascript 复制代码
Function.prototype.unCurrying= function() {
    var that = this;
    return function() {
        return Function.prototype.call.apply(that, arguments);
    }
}

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

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

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

通用反柯里化函数

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

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

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

perl 复制代码
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的例子

javascript 复制代码
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进行反柯里化!

javascript 复制代码
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,例如:

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

uncurrying 函数进阶

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

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

也可以这么写:

javascript 复制代码
var uncurrying= function (fn) {
    return function () {        
        return Function.prototype.call.apply(fn,arguments);
    }
};

还可以这么写

javascript 复制代码
var uncurrying=Function.prototype.bind.bind(Function.prototype.call);

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

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

相关推荐
一路向前的月光44 分钟前
react(9)-redux
前端·javascript·react.js
大数据追光猿1 小时前
Python中的Flask深入认知&搭建前端页面?
前端·css·python·前端框架·flask·html5
莫忘初心丶1 小时前
python flask 使用教程 快速搭建一个 Web 应用
前端·python·flask
xw52 小时前
Trae初体验
前端·trae
横冲直撞de2 小时前
前端接收后端19位数字参数,精度丢失的问题
前端
我是哈哈hh2 小时前
【JavaScript进阶】作用域&解构&箭头函数
开发语言·前端·javascript·html
摸鱼大侠想挣钱2 小时前
ActiveX控件
前端
谢尔登2 小时前
Vue 和 React 响应式的区别
前端·vue.js·react.js
后端小肥肠2 小时前
【AI编程】Java程序员如何用Cursor 3小时搞定CAS单点登录前端集成
前端·后端·cursor
酷酷的阿云2 小时前
Vue3性能优化必杀技:useDebounce+useThrottle+useLazyLoad深度剖析
前端·javascript·vue.js