4

I really could use an explanation from anybody.

Seriously confused about this code. Especially line 2.

Code source is https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/slice

var unboundSlice = Array.prototype.slice;
var slice = Function.prototype.call.bind(unboundSlice);

function list() {
  return slice(arguments);
}

var list1 = list(1, 2, 3); // [1, 2, 3]
W3Guy
  • 657
  • 1
  • 7
  • 16
  • 1
    It hard-wires (so to speak) the `.call` part of `Array.prototype.slice.call`, turning a method of an array instance into a function that instead takes an array as an argument. – Jared Smith Jun 23 '16 at 19:57
  • People are very quick to call Duplicate. This is a specific question for very specific code example from the MDN site. It's a good question, and the answer is related to the code in question. – Drenai Jan 11 '17 at 11:30

1 Answers1

7

var slice = Function.prototype.call.bind(unboundSlice);

Pass unboundSlice as context (this operator) that function call will execute. So when execute: list(1,2,3) <=>

slice([1,2,3]) <=>

excecute call with context unboundSlice and parameter arguments: unboundSlice.call(arguments) <=>

execute unboundSlice function with context arguments (array [1,2,3]):[1,2,3].unboundSlice() <=>

execute: [1,2,3].slice() <=>

[1, 2, 3]

Cuong Ta
  • 383
  • 1
  • 6