覆盖参数对象的JavaScript函数 [英] Overwriting arguments object for a Javascript function
问题描述
如果我有以下几点:
// Clean input.
$.each(arguments, function(index, value) {
arguments[index] = value.replace(/[\W\s]+/g, '').toLowerCase();
});
请问这是一个糟糕的事是什么?我没有更多的使用在功能上未清洗参数,这将是很好不创造参数的无用副本
只是为了使用它们,但是否有任何负面影响要这么做?
Would that be a bad thing to do? I have no further use for the uncleaned arguments in the function, and it would be nice not to create a useless copy of arguments
just to use them, but are there any negative effects to doing this?
理想我想这样做,但我猜这个过程中遇到问题,因为参数
是不是一个真正的数组:
Ideally I would have done this, but I'm guessing this runs into problems since arguments
isn't really an Array:
arguments = $.map(arguments, function(value) {
return value.replace(/[\W\s]+/g, '').toLowerCase();
});
感谢您的任何意见。
Thanks for any input.
编辑:我刚刚意识到,这两个现在里面自己的职能,所以参数对象发生了变化。任何方式做到这一点不会造成不必要的变量?
I've just realized that both of these are now inside their own functions, so the arguments object has changed. Any way to do this without creating an unnecessary variable?
推荐答案
我也不会想到,参数[I] =值;
工作,因为同样的原因, (这是不是一个真正的数组)。
I would not have thought that arguments[i] = value;
works, because of the same reason (it is not a real array).
您真的应该考虑到清洁值分配给一个新的变量:
You really should consider to assign the cleaned values to a new variable:
var cleaned_args = $.map(arguments, function(value) {
return value.replace(/[\W\s]+/g, '').toLowerCase();
});
在这里引入一个新的变量是没有必要。通常,你不直接对参数
无论如何操作(因为它的缺点像你已经发现了一个),但它首先转换为真正的数组(这将涉及反正新变量)。
Introducing a new variable here is not unnecessary. Most often you don't directly operate on arguments
anyway (because of its shortcomings like the one you already discovered), but convert it to a real array first (which will involve a new variable anyway).
关于您的编辑:
右键,第一个是行不通的,因为参数
指的是匿名函数的参数。
Right, the first one would not work because arguments
refers to the arguments of the anonymous function.
这篇关于覆盖参数对象的JavaScript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!