bind实现函数柯里化来实现的
context 是个函数
Function.prototype.bind = function (context) {
let _this = this || window;
let _args = Array.prototype.slice.call(arguments,1);
return function() {
_this.apply(context,_args);
}
}
call实现
Function.prototype.mycall = function(context) {
if(typeof this !== 'function') {
throw new TypeError('not funciton');
}
let _args = [...arguments].slice(1);
context = context || window;
context.fn = this;
let result = context.fn(..._args);
delete context.fn;
return result;
}
apply实现
Function.prototype.myApply = function(object,arr){
object.__proto__._fn = this;
var result = object._fn(...arr);
delete object.__proto__._fn;
return result;
}
add(1)(1,2);函数柯里化实现
function add() {
let _args = [...arguments];
let _adder = function() {
_args = [..._args,...arguments];
return _adder;
}
_adder.toString = function() {
return _args.reduce((a,b) => a+b);
}
return _adder;
}