ES5实现 ```javascript (function(){ if (!Function.prototype.bind) { let slice = Array.prototy.slice
Funcion.prototype.bind = function() {
let that = this; let context = arguments[0]; if (typeof that !== ‘function’) {
throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable")
}
let outerArgs = slice.call(arguments, 1);
return function() {
let innerArgs = slice.call(arguments);
let allArgs = outerArgs.concat(innerArgs);
return context.apply(that, allArgs)
; }
} } })()
2. ES6实现
```javascript
(function() {
if(!Function.prototype.bind) {
Function.prototype.bind = function(context, ...outerArgs) {
let that = this;
if(typeof that !== 'function') {
throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
}
return function(innerArgs) {
return that.apply(context, [...outerArgs, ...innerArgs]);
}
}
}
})()