1

I'm studying underscore delay to better my JS skills, and I'm trying to explain every line to myself to make sure I understand. Could someone explain what this line is doing?

var args = Array.prototype.slice.call(arguments, 2);

Is it taking the first 2 values of the arguments array and making it equal to var 'args'?

also,

shouldn't "wait" be a numerical value like 3000 milliseconds? wondering why it's spelled out?

_.delay = function(func, wait) {
    var args = Array.prototype.slice.call(arguments, 2); 
    setTimeout(function(){
        func.apply(this, args);
    }, wait);
};
georg
  • 211,518
  • 52
  • 313
  • 390
Rory Perro
  • 441
  • 2
  • 7
  • 16
  • 1
    `Array.prototype.slice.call(arguments, 2)` is extracting elements starting from 2 (index) – Satpal Aug 05 '15 at 07:18
  • @Satpal Better state that `arguments` is not `array`. – fuyushimoya Aug 05 '15 at 07:20
  • 3
    This is fourth question row, I would recommend you to go through official dos first [Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) – Satpal Aug 05 '15 at 07:22
  • The `arguments` object is an Array-like object corresponding to the arguments passed to a function. https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/arguments – Satpal Aug 05 '15 at 07:23
  • 1
    `wait` is spelled out because it's a parameter you'd pass to `_.delay()`: `_.delay(function(){/*DoStuff*/}, 1000)` – Cerbrus Aug 05 '15 at 07:29
  • See also [How to get a slice from “arguments”](http://stackoverflow.com/q/9510094/1048572) or [How can I convert the “arguments” object to an array in JavaScript?](http://stackoverflow.com/q/960866/1048572) – Bergi Aug 05 '15 at 07:31
  • @Bergi: Dupe doesn't answer the (trivial) second half of the question. – Cerbrus Aug 05 '15 at 07:31
  • @Cerbrus: People asking multiple questions in a single post never stopped me from closing it :-) – Bergi Aug 05 '15 at 07:34

2 Answers2

1
var args = Array.prototype.slice.call(arguments, 2); 

This line takes all but the first 2 parameters that are passed to _delay, to pass those on to the setTimeout.

wait is spelled out because it's a parameter you'd pass to _.delay():

_.delay(function(){
    // Do stuff
}, 1000)`;

This parameter is then also passed on to the setTimeout

Basically, that implementation of _.delay does exactly the same as setTimeout does, with the disadvantage that it doesn't return an timeout identifier you could use to clear the timeout.

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
1

var args = Array.prototype.slice.call(arguments, 2); is extracting (returning) all elements from the array starting at the second index. Try this in the browser console:

Array.prototype.slice.call([1, 2, 3, 4, 5], 2)
// Output: [3, 4, 5]

Regarding wait: since wait is one of the passed parameters, it is spelled out. The content of wait should be an integer (in milliseconds), since that's what setTimeout(callback, timeInMilliseconds) is expecting as second parameter.

Claudio Bredfeldt
  • 1,423
  • 16
  • 27