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

相关推荐
ZJPRENO1 小时前
WorkBuddy 从零到一|混元4 Flash 实战手记-提醒小工具开发
前端
用户302822530681 小时前
AI 视频图片运镜为什么会抖?帧率拉高也救不了多次取整
前端·人工智能·typescript
前端码农人生1 小时前
前端转 Agent:真正要重写的不是接口层,是交互层
前端
AI2中文网1 小时前
App Inventor 做的表单怎么导出 PDF?WebViewer + JavaScript 在本地生成并保存 PDF 的完整示例
android·开发语言·javascript·pdf·鸿蒙·app inventor
孟陬2 小时前
Foobar 技术周刊 #003:黄仁勋的三段吃瓜和回归,抛开事实不谈(嘿嘿)让全人类 AGI 进程得以继续
前端·后端·程序员
涛涛ing2 小时前
别再用 AI 写计算逻辑了:你的页面卡死,连鼠标都点不动
前端
OpenTiny社区3 小时前
当 AI 开始写 UI,我们缺的不是模型,是"设计词汇"
前端·github·ai编程
Amos_Web3 小时前
Rspack 源码解析(三十八):SubresourceIntegrityPlugin 如何跨四个插件协作生成 integrity
前端
Amos_Web4 小时前
通过 Rspack 源码学 Rust(八):struct 保存数据,impl 定义怎样使用数据
前端
anyup4 小时前
2027 年初,uni-app App 端停更倒计时,老项目该何去何从?
前端·vue.js·uni-app