admin管理员组文章数量:1327660
I have a JavaScript function that takes two required parameters and then arbitrarily many optional parameters.
function myFunction(required1, required2) {
var more = [];
for (var i = 2; i < arguments.length; ++i)
more.push(arguments[i]);
// ...
}
Now, I like enforcing a consistent style through all my code. Since my site uses jQuery, and jQuery favors using $.each
and $.map
over explicit loops, I want to get rid of the explicit loop in myFunction
. However, I cannot use either $.each
or $.map
because I don't want to copy the whole argument list, lest I do the following:
var more = $.map(arguments, function(argument, index) {
return (index < 2) ? null : [argument];
});
Which is, of course, a very bad idea, because testing whether index < 2
in every iteration is unnecessary.
I would really like to be able to extract a subset of the arguments
object into a new array, using a standard function. However, because arguments
is not an array, I cannot slice
it.
Is there any other way I could extract into an array all arguments but the two first ones, without using an explicit loop, and without losing efficiency?
I have a JavaScript function that takes two required parameters and then arbitrarily many optional parameters.
function myFunction(required1, required2) {
var more = [];
for (var i = 2; i < arguments.length; ++i)
more.push(arguments[i]);
// ...
}
Now, I like enforcing a consistent style through all my code. Since my site uses jQuery, and jQuery favors using $.each
and $.map
over explicit loops, I want to get rid of the explicit loop in myFunction
. However, I cannot use either $.each
or $.map
because I don't want to copy the whole argument list, lest I do the following:
var more = $.map(arguments, function(argument, index) {
return (index < 2) ? null : [argument];
});
Which is, of course, a very bad idea, because testing whether index < 2
in every iteration is unnecessary.
I would really like to be able to extract a subset of the arguments
object into a new array, using a standard function. However, because arguments
is not an array, I cannot slice
it.
Is there any other way I could extract into an array all arguments but the two first ones, without using an explicit loop, and without losing efficiency?
Share Improve this question asked May 5, 2011 at 16:41 isekaijinisekaijin 19.8k19 gold badges87 silver badges154 bronze badges2 Answers
Reset to default 10Using the slice
method:
var optional_arguments = Array.prototype.slice.call(arguments, 2);
We have to call it from Array.prototype
because, even though arguments
is array-like, it doesn't actually have a slice
method.
The arguments of a function can be treated like an array with jQuery, for instance the following works perfectly fine:
function something(ar1,ar2){
var args = $(arguments).slice(2);
$(args).each(function(i){
$('#stuff').append(args[i]+'<br />');
});
}
$(function(){
something('one','two','three','four');
});
And to tailor it to your function above it would look like this:
function myFunction(required1, required2) {
var more = [];
var args = $(arguments).slice(2);
$(args).each(function(i){
more.push(args[i]);
});
}
本文标签:
版权声明:本文标题:javascript - Copying a subset of the arguments object, without using an explicit loop - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742227999a2436686.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论