Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function apply with arguments using slice

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?

like image 759
letiagoalves Avatar asked Dec 04 '13 11:12

letiagoalves


People also ask

Can you slice arguments JavaScript?

All you know that arguments is a special object that holds all the arguments passed to the function. And as long as it is not an array - you cannot use something like arguments. slice(1) .

What does apply () do in JavaScript?

Summary. The apply() method invokes a function with a given this value and arguments provided as an array. The apply() method is similar to the call() method excepts that it accepts the arguments of the function as an array instead of individual arguments.

What is the difference between using call () and apply () to invoke a function with multiple arguments?

The Difference Between call() and apply() The difference is: The call() method takes arguments separately. The apply() method takes arguments as an array. The apply() method is very handy if you want to use an array instead of an argument list.

What is the use function prototype apply method?

prototype. apply() The apply() method calls the specified function with a given this value, and arguments provided as an array (or an array-like object).


2 Answers

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.

like image 118
Bergi Avatar answered Oct 23 '22 23:10

Bergi


slice is needed because function.apply requires an array, arguments is not a real array but can be converted to one using slice.

The arguments object is not an Array. It is similar to an Array, but does not have any Array properties except length. For example, it does not have the pop method. However it can be converted to a real Array:

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

this is one of the ugly parts of javascript

like image 37
roo2 Avatar answered Oct 23 '22 23:10

roo2