只是看着Polyfill' https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind部分,我注意到一些我认为可能是错误的,或者是我的错误:
Function.prototype.bind = function (oThis) {
if (typeof this !== "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");
}
var aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,
fNOP = function () {},
fBound = function () {
return fToBind.apply(this instanceof fNOP && oThis
? this
: oThis,
aArgs.concat(Array.prototype.slice.call(arguments)));
};
fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();
return fBound;
};
关于绑定函数(在bind函数中称为fBound),apply语句覆盖了可能实例化绑定函数的情况。然而,它所做的检查是oThis对#bind的参数也已被提供....如果是,它会忽略它,选择实例引用它...当然所有那些都需要它们是没有的fNOP实例&安培;&安培;这个布尔检查?
return fToBind.apply(this instanceof fNOP ? this : oThis
等。毕竟,如果没有oThis,则无法实例化绑定函数,而AFAIK不是ES5规则。还有其他人在实施时抓头?或者我的错误?干杯!