I am calling a method using apply and I do not know how many arguments I will be passing:
At the moment my code looks like this:
selectFolder: function(e){
e.preventDefault();
this.addSelectedClass.apply(this, Array.prototype.slice.call(arguments));
},
The only reason I am using Array.prototype.slice is because it is in most examples.
Why would I not just pass arguments like this:
this.addSelectedClass.apply(this, arguments);
I am calling a method using apply and I do not know how many arguments I will be passing:
At the moment my code looks like this:
selectFolder: function(e){
e.preventDefault();
this.addSelectedClass.apply(this, Array.prototype.slice.call(arguments));
},
The only reason I am using Array.prototype.slice is because it is in most examples.
Why would I not just pass arguments like this:
this.addSelectedClass.apply(this, arguments);
Share
Improve this question
asked Aug 20, 2012 at 14:59
dagda1dagda1
29k67 gold badges255 silver badges477 bronze badges
2 Answers
Reset to default 6arguments
with apply()
are fine
When calling apply
on a function it's ok to just use original arguments
. SO in your example you can easily replace the line with this one:
this.addSelectedClass.apply(this, arguments);
arguments
with call()
are not fine
But if you'd be calling call
then you should convert arguments
to an actual array (which is done by a slice()
call) if you'd want to pass your function an array of arbitrary objects/values. That's why call()
is usually used when you know exactly the number of arguments you'd want to pass and pass them individually.
arguments
are not an actual array object. They seem to be (have length
property for instance), but they're not.
Arguments on Mozilla Deveveloper Network
A bit of explanation to make it pletely clear
Documentation related to apply()
and call()
looks like this:
func.apply(thisArg[, argsArray]);
func.call(thisArg[, arg1[, arg2[, ...]]]);
We may execute the same function using any of these.
When using
apply()
we can pass all arguments at once.slice()
is usually used to omit a particular argument from the call or to convertarguments
to actual array.When using
call()
we pass individual arguments and as many as we need to. If we'd provide arguments converted to an array, our called function would get one parameter of type array with all arguments supplied in that particular array.
I hope this clears thing up even more.
Because arguments
is not an array. Running Array.prototype.slice
makes it a normal array with functions like map
and forEach
. Basically anything in Array.prototype
. arguments
is, without this, just an object with a length
property.
In your particular case, passing just arguments
would probably work.