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]
1
  • 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. Commented Jun 23, 2016 at 19:57

1 Answer 1

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]

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.