This is an older version of bind method polyfill from MDN:
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 &&
oThis ? this : oThis
),
aArgs.concat( Array.prototype.slice.call( arguments ) )
);
}
;
fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();
return fBound;
};
}
And I'm confused with this instanceof fNOP here. I know sometimes we may call the fBound return by bind method as a constructor function. Like this:
const fn2 = fn1.bind()
new fn2()
So I think the purpose of this instanceof fNOP is to help judge how we call the function return by bind method and do something relatively. But why no just check this instanceof fBound here? If the fBound is called as a constructor function, then this must be an instance of fBound, right?
PS: The latest version of bind polyfill has turned the this instanceof fNOP check into the fNOP.prototype.isPrototypeOf(this) check, but I think the purpose is the same. Anyway, it still check fNOP instead of fBound, which I can't quite understand.