-1

I learn js and now I am learning prototypes. I found this code:

function list() {
  return Array.prototype.slice.call(arguments, 0);
}

var list1 = list(1, 2, 3); // [1, 2, 3]

Could you tell me what does mean 0 here. I saw the code with (arguments, 1) and did not understand.Could you help me in this question? Thanks.

Timur
  • 211
  • 1
  • 11

3 Answers3

2

As documented here Array.prototype.slice expects at least one argument, start, which is the position at which to start "slicing". You would call this method like this: [1, 2, 3].slice(0) and you'd get a copy of the array, with the same elements.

method.call is a way to call a method of an object and specify the this (the object itself) as an argument (documented here). So instead of [1, 2, 3].slice(0) you can do Array.prototype.slice.call([1, 2, 3], 0), where [1, 2, 3] will be this inside Array.prototype.slice, meaning the array instance it acts upon, all subsequent parameters (0 in this case) are passed to the called function.

arguments is a "magic" variable inside any function that is an array of all the arguments passed to the function. Note that in JavaScript you can call a function with any number of extra arguments. In your case arguments will be [1, 2, 3].

So to answer your initial question: 0 is the position at which to start the "slicing".

Sergiu Paraschiv
  • 9,929
  • 5
  • 36
  • 47
  • Very good answer however I like to motivate people reading the documentation. Because the Mozilla documentation is already fantastic and very good understandable plus it gives good examples. – thex Mar 20 '19 at 15:13
  • 1
    Me too. In this case I considered a bigger push would be helpful though :) More links = more _guided_ reading material IMHO. – Sergiu Paraschiv Mar 20 '19 at 15:14
  • 1
    Yeah you are probably right ;), I hope your work is honored! – thex Mar 20 '19 at 15:16
1

Your code calls the function slice() with the parameter 0. Please also take a look here for more information on call and what it does I'm not going to reiterate the functionality here. Furthermore I suggest the slice() documentation here. In your case slice(0) does "nothing" because a copy of the original array is returned. The call slice(1) would return all elements except the first one.

thex
  • 590
  • 2
  • 12
0

Probably you've become confused by the arguments variable and call() function.

Try to debug and all becomes more clearer:

function list() {
  console.log('Function hidden args: ', arguments)// Output: Function hidden args: [1, 2, 
      //3, callee: ƒ, Symbol(Symbol.iterator): ƒ]
  return Array.prototype.slice.call(arguments, 0);
}
var list1 = list(1, 2, 3); // [1, 2, 3]

So this return Array.prototype.slice.call(arguments, 0); can be converted to

[/*array items here*/].slice(0)

0 means here a starting point from existing array where new array will be created. For example:

console.log([1,2,3,4,5].slice(1)); // [2, 3, 4, 5]
console.log([1,2,3,4,5].slice(2)); // [3, 4, 5]
console.log([1,2,3,4,5].slice(3)); // [4, 5]
StepUp
  • 36,391
  • 15
  • 88
  • 148