JavaScript 中 apply() 的应用技巧

2,965 阅读1分钟

最近在看JavaScript设计模式,其中有一些巧妙的函数。

部分修改后记录在此,顺便加上自己写出的一些好玩的函数。

apply实现call
Function.prototype.call = function () {
    var ctx = [].shift.apply(arguments)
    return this.apply(ctx, arguments)
    }
apply实现bind
Function.prototype.bind = function () {
    var ctx = [].shift.apply(arguments),
        args = [].slice.apply(arguments),
        self = this
            return function () {
        return self.apply(ctx, args.concat([].slice.apply(arguments)))
    }
    }
实现函数柯里化
Function.prototype.currying = function () {
    var args = [],
        self = this
            return function () {
        if (arguments.length === 0) {
            return self.apply(this, args)
        } else {
            [].push.apply(args, arguments)
            return arguments.callee
        }
    }
    }
    //用法
var add = function () {
    var sum = 0
    for (var i = 0; i < arguments.length; i++) {
        sum += arguments[i]
    }
    return sum
    }.currying()
    add(2) //并未求值
add(3, 3) //并未求值
add(4) //并未求值
console.log(add()) //12
严格模式不能使用arguments.callee, 稍微改一下
Function.prototype.currying = function () {
    var args = [],
        self = this
            var f = function () {
        if (arguments.length === 0) {
            return self.apply(this, args)
        } else {
            [].push.apply(args, arguments)
            return f
        }
    }
    return f
    }
实现函数反柯里化
Function.prototype.uncurrying = function () {
    var self = this
    return function () {
        var obj = [].shift.apply(arguments)
        return self.apply(obj, arguments)
    }
    }
    // 用法
var push = Array.prototype.push.uncurrying()
var obj = {}
push(obj, '嘿')
console.log(obj) //{0: "嘿", length: 1}
另一种方法:call和apply连用实现函数反柯里化
Function.prototype.uncurrying = function () {
    var self = this
    return function () {
        return Function.prototype.call.apply(self, arguments)
        //有点绕,其实就是return self.call(args[0], args[1], args[2]...)
    }
    }
为数组添加max函数
Array.prototype.max = function () {
    return Math.max.apply(null, this)
    }
    console.log([1, 3, 5, 2].max()) //5