0

Given the following code:

/*jslint white:true, devel:true*/
var foo = function () {
  'use strict';
  console.log(
    Array.prototype.slice.call(
      /* How can I send more arguments to call after 'arguments'? */
      arguments,
      /*
       This function gets sent to slice.
       If I sent a '2' it would start the array at the third index.
       */
      function () {
        console.log("bar");
      }
    )
  );
};

foo(1, 2, 3, 4, 5, 6);

I am trying to better understand how the parameters work in this context. It seems that the second parameter is always sent to slice.

Is there any way to send another parameter to call in this context? Or would I need to rewrite the code to .slice() separately from the .call()?

UPDATE:

I am not really sure why this was down-voted. My conceptual misunderstanding was that .slice was taking over, rather than being passed all arguments through .call(). Thanks to Quentin for clarifying it.

ryanpcmcquen
  • 6,285
  • 3
  • 24
  • 37

1 Answers1

2

It seems that the second parameter is always sent to slice.

The first argument to call is the value of this that slice should work on. i.e. It acts like arguments.slice(...) if arguments were an array instead of an array-like object that didn't have a slice method of its own.

The second argument to call is the first argument to slice. i.e. the offset in the array to start slicing from. It doesn't make sense to pass a function as you do in the question.

Is there any way to send another parameter to call in this context?

Yes. Just do it.

The third argument to call is the second argument to slice. i.e. the index to stop slicing at.

The fourth argument to call is the third argument to slice, but slice only takes two arguments so it wouldn't make sense to pass any more.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • It finally makes sense. Thanks for answering this down-voted question. I still learned something here despite losing some Stack Overflow points. – ryanpcmcquen Apr 02 '16 at 22:13