【发布时间】:2016-12-28 15:23:49
【问题描述】:
为什么我们需要在这里使用 instanceOf ,在这种情况下结果将是 true ?
if (!Function.prototype.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
? this
: oThis,
aArgs.concat(Array.prototype.slice.call(arguments)));
// 这里为什么要使用instanceOf };
if (this.prototype) {
// Function.prototype doesn't have a prototype property
fNOP.prototype = this.prototype;
}
fBound.prototype = new fNOP();
return fBound;
};
}
//哪种情况下结果为真?
【问题讨论】:
-
我认为当您在已绑定的函数上调用
.bind()时可能会处理它,但我尝试删除测试并且它的工作原理相同。 -
一般它 if(this instanceof fNOP) return false 为什么使用它?看不懂
标签: javascript bind instanceof polyfills