curry&unCurry函数

unCurry函数与curry函数区别:
curry化函数:固定部分参数,返回一个接受剩余参数的新函数,目的是为了缩小适用范围,创建一个针对性更强的函数。 unCurry化函数:扩大适用范围,创建一个应用范围更广的函数。使本来只有特定对象才适用的方法,扩展到更多的对象。
unCurry函数  参考:https://www.cnblogs.com/pigtail/p/3450852.html   https://juejin.im/post/5b561426518825195f499772#heading-7
方法一:
class Toast{ constructor(){
this.prompt='' } show(){ console.log(this.prompt); } } var temObj={prompt:'tem'} Toast.prototype.show.call(temObj) function unCurry(fn){ return function(context){ var args=Array.prototype.slice(1)||[]; fn.apply(context,args) } } var unCurryShow=unCurry(Toast.prototype.show); unCurryShow(temObj)
 
 
方法2
Function.prototype.unCurrying = function(){
    var self = this;
    return function(){
        return Function.prototype.call.apply(self, arguments);
    }
    //return this.call.bind(this);
}

// 使用
var objShow = Toast.prototype.show.unCurrying();
objShow(obj);

 应用:

Function.prototype.unCurry=function(){
    return this.call.bind(this)
}
var objPush=Array.prototype.push.unCurry();
var temObj={};
objPush(temObj,'a','b')  
 
 
var toUpperCase = String.prototype.toUpperCase.unCurrying();
console.log(toUpperCase('avd')); // AVD

function AryUpper(ary) {
    return ary.map(toUpperCase);
}

console.log(AryUpper(['a', 'b', 'c'])); // ["A", "B", "C"]
var call = Function.prototype.call.unCurrying();
function $(id) {
    return this.getElementById(id);
}
var demo = call($, document, 'demo');
console.log(demo);

//Function.prototype.call.call(fn,context,...args)
var objToString=Object.prototype.toString.unCurry()
var fn1=function(){}
objToString(objToString)

 

猜你喜欢

转载自www.cnblogs.com/little-ab/p/11123282.html
今日推荐