Reputation: 15034
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;
};
}
I was looking at the source of the bind function, i was just thinking on why they are doing an Array.prototype.slice.call
when i can directly do a slice to my arguments
.
Upvotes: 0
Views: 229
Reputation: 145398
Because arguments
is not a pure JavaScript array, but array-like object. So in order to make changes to it, you have to transform it to the real array using Array.prototype.slice.call
.
From MDN:
The
arguments
object is not an Array. It is similar to an Array, but does not have any Array properties exceptlength
.
Upvotes: 2