Function.prototype.call = function () {
var ctx = [].shift.apply(arguments)
return this.apply(ctx, arguments)
}
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
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}
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]...)
}
}
Array.prototype.max = function () {
return Math.max.apply(null, this)
}
console.log([1, 3, 5, 2].max()) //5
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有