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函数的 反柯里化,就先写到这里!

相关推荐
|晴 天|37 分钟前
Vue 3 + TypeScript + Element Plus 博客系统开发总结与思考
前端·vue.js·typescript
猫3281 小时前
v-cloak
前端·javascript·vue.js
AC赳赳老秦1 小时前
OpenClaw二次开发实战:编写专属办公自动化技能,适配个性化需求
linux·javascript·人工智能·python·django·测试用例·openclaw
旷世奇才李先生1 小时前
Vue 3\+Vite\+Pinia实战:企业级前端项目架构设计
前端·javascript·vue.js
Ulyanov2 小时前
《PySide6 GUI开发指南:QML核心与实践》 第二篇:QML语法精要——构建声明式UI的基础
java·开发语言·javascript·python·ui·gui·雷达电子对抗系统仿真
聚美智数3 小时前
企业实际控制人查询-公司实控人查询
android·java·javascript
SoaringHeart3 小时前
Flutter进阶:用OverlayEntry 实现所有弹窗效果
前端·flutter
IT_陈寒5 小时前
Vite静态资源加载把我坑惨了
前端·人工智能·后端
herinspace5 小时前
管家婆实用贴-如何分离和附加数据库
开发语言·前端·javascript·数据库·语音识别
小码哥_常5 小时前
从MVC到MVI:一文吃透架构模式进化史
前端