0
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.

theJava
  • 14,620
  • 45
  • 131
  • 172

1 Answers1

2

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 except length.

VisioN
  • 143,310
  • 32
  • 282
  • 281
  • How does the Array.prototype.slice.call clone that into an array object.... how is arguments an array-link object. – theJava Mar 15 '13 at 12:21
  • 1
    @theJava The best description of its work is here: http://stackoverflow.com/a/7057090/1249581. – VisioN Mar 15 '13 at 12:24
  • MDN includes a bit about the array-like behavior in the slice doc, FWIW: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice#Array-like – James Manning Aug 29 '13 at 03:06