bind 实现

用法

MDN bind

极简实现

1
2
3
4
5
6
Function.prototype.bind = function(obj, ...args) {
var fn = this;
return function() {
return fn.apply(obj, args)
}
}

简易实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Does not work with `new funcA.bind(thisArg, args)`
if (!Function.prototype.bind) (function(){
// Is this an error? We are invoking <call.bind> method before it's defined.
var slice = Array.prototype.slice.call.bind(Array.prototype.slice);
Function.prototype.bind = function() {
var thatFunc = this, thatArg = arguments[0];
var args = slice(arguments, 1);
if (typeof thatFunc !== 'function') {
// closest thing possible to the ECMAScript 5
// internal IsCallable function
throw new TypeError('Function.prototype.bind - ' +
'what is trying to be bound is not callable');
}
return function(){
var funcArgs = args.concat(slice(arguments))
return thatFunc.apply(thatArg, funcArgs);
};
};
})();