Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why apply() here takes only one argument instead of two?

Tags:

javascript

I'm reading the book Javascript: The Good Parts. And I'm confused by the following code.

Function.method('curry', function (  ) {
    var slice = Array.prototype.slice,
        args = slice.apply(arguments),
        that = this;
    return function (  ) {
        return that.apply(null, args.concat(slice.apply(arguments)));
    };
});

Where is the null in slice.apply(arguments)?

like image 298
amazingjxq Avatar asked Oct 31 '11 01:10

amazingjxq


2 Answers

arguments is being passed as the context (this), not the function's arguments.

It's equivalent to arguments.slice(), except that arguments.slice() doesn't exist.

like image 122
SLaks Avatar answered Sep 30 '22 03:09

SLaks


That's the equivalent of calling slice() on an array with no arguments - i.e. it returns an array with all the elements of the original array. In this case, 'arguments' is not a true array, so calling Array.prototype.slice on it in effect turns it into one.

like image 35
Benjie Avatar answered Sep 30 '22 02:09

Benjie