Ben Aston Ben Aston - 1 year ago 177
Javascript Question

Explanation of a line in MDN bind polyfill

The MDN bind polyfill is shown below.

I am trying to work out the purpose of

this instanceof fNOP ? this : oThis

in the

I can't get my head around it. Can someone help shed some light?

Function.prototype.bindMdn = 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 =, 1)
, fToBind = this
, fNOP = function() {}
, fBound = function() {
return fToBind.apply(this instanceof fNOP ? this : oThis, aArgs.concat(;
if (this.prototype) {
// Function.prototype doesn't have a prototype property
fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();
return fBound;

It seems to be a short-circuit if an instance of the bound function is supplied as the target when invoking the bound function, but the typeof check should catch this, so I don't understand its presence.

Link to the MDN page:

Edit: This is a different question from the suggested duplicate. The suggested duplicate asks why
is needed. I fully grok that.

This question is why the
check is needed and what function it serves. I present my short-circuit hypothesis above, together with a reason why that doesn't fully make sense.

Answer Source

If you use the result of a .bind to create a new instance with new:

 function TestClass(a,b,c,d) {

 var TestClassBound = TestClass.bindMdn(null, 1, 2, 3);

 new TestClassBound();

Then this instanceof fNOP is true.

The typeof this !== 'function' is just there to test if it was called a regular way on a function and not with call or apply or to make sure it was not copied to another objects prototype. So it only prevent something like"Not a function", 1, 2, 3);


var testObj = {};
testObj.bind = Function.prototype.bind;


For every regular call of bind on a function the typeof this will always be function.

So the typeof this !== 'function' is to check if the object bind is called is really a function.

And the this instanceof fNOP within the fBind ensures that the behaviour is correct when the result of the binding is used.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download