admin管理员组文章数量:1334909
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.
本文标签: javascriptWhy use Arrayprototypeslicecall with argumentsStack Overflow
版权声明:本文标题:javascript - Why use Array.prototype.slice.call with arguments - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742331123a2454717.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论