Function apply with arguments using slice

angularjs, arrays, function, javascript, slice

Solution

You don't really need it, passing `arguments` to `apply` directly is fine.

It is specified to be allowed in EcmaScript 3, and EcmaScript 5 even allows any array-like objects not only arrays and arguments objects. Maybe it was needed to be backwards-compatible with even earlier or buggy JS implementations. Also, (as you see from the other answers), some people don't know about this fact and think they would need to pass actual arrays - so they are unnecessary careful.

Problem

Looking at this tutorial I saw the following code suggestion in one comment: ``` init:function(callback){ var that =this ; return $http.jsonp(this.url).success( function(data){ that.availableGenres = that.getGenres(data); that.results = that.getResults(data); if(callback)callback.apply(null,[].slice.call(arguments)) } ) } ``` But this line `callback.apply(null,[].slice.call(arguments))` looks weird to me. Why not just: `callback.apply(null, arguments)`? Because I don't like when I don't understand the point of something I played around with this Fiddle to understand the need of `slice` function in there. But it gives me the same result and I still do not get it. Anyone knows why `slice` is needed?

Original source