MDN's polyfill for Function.prototype.bind (pasted below), uses the comma operator. Since the comma operator returns the last operand, and the first operand (oThis
) doesn't do anything (like call a function, or make an assignment), it looks to be pointless. But I'm going to assume MDN knows what it's doing, so: What useful trick is the comma operator doing in this case?
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, // <-- what's going on here?
aArgs.concat(Array.prototype.slice.call(arguments)));
};
fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();
return fBound;
};
}
To be clear, I'm suggesting that you could write fBound
like this, and have it do the same thing:
fBound = function () {
return fToBind.apply(this instanceof fNOP && oThis
? this
: aArgs.concat(Array.prototype.slice.call(arguments)));
};