Javascript 更改通过函数签名命名的所有变量的值
假设我有一个有50个参数的函数,我需要修改在函数签名中创建的每个命名变量的值 他不是50个论点,而是一个只有4个论点的例子:Javascript 更改通过函数签名命名的所有变量的值,javascript,ecmascript-next,Javascript,Ecmascript Next,假设我有一个有50个参数的函数,我需要修改在函数签名中创建的每个命名变量的值 他不是50个论点,而是一个只有4个论点的例子: //这些字符串中的每一个都用有意的和不必要的空格填充: let show=“show”; 让我=“我”; 让“the”; let bunny=“bunny”; 函数showMeTheBunny(show,me,the,bunny) { //我想删减每个论点,而不必这样做: show=show.trim(); me=me.trim(); the=the.trim(); b
//这些字符串中的每一个都用有意的和不必要的空格填充:
let show=“show”;
让我=“我”;
让“the”;
let bunny=“bunny”;
函数showMeTheBunny(show,me,the,bunny)
{
//我想删减每个论点,而不必这样做:
show=show.trim();
me=me.trim();
the=the.trim();
bunny=bunny.trim();
//上述行,在此函数中,
//是我想用循环替换的(如果可能的话)
return`${show}${me}${the}${bunny}:你是说这个
将迭代参数转换为数组并映射它
Array.from(arguments)
或[…arguments]
将在此处工作
这实际上并没有像TJ所指出的那样修改实际的参数数组
//这些字符串中的每一个都用有意的和不必要的空格填充:
let show=“show”;
让我=“我”;
让“the”;
let bunny=“bunny”;
函数showMeTheBunny(show,me,the,bunny){
返回Array.from(arguments).map(el=>el.trim()).join(“”+):您说过要修改“命名变量”的值,所以我假设您指的是形式参数(show
,me
,等等)
arguments对象可以访问传递到函数中的所有参数,但它似乎没有提供更改命名变量本身值的方法
确实如此,但仅在松散模式下,而非严格模式下:
function showMeTheBunny(show, me, the, bunny)
{
for (let n = 0; n < arguments.length; ++n)
{
arguments[n] = arguments[n].trim();
}
return `${show} ${me} ${the} ${bunny}: The properties of an arguments
object are actually setters. If you reassign a property on the argument, the corresponding variable name will change as well. So you can iterate through the arguments
and reassign them:
// Each of these strings are padded with intentional and unnecessary whitespace:
let show = " show ";
let me = " me ";
let the = " the ";
let bunny = " bunny ";
function showMeTheBunny(show, me, the, bunny)
{
[...arguments].forEach((arg, i) => {
arguments[i] = arg.trim();
});
return `${show} ${me} ${the} bunny`;
}
console.log(showMeTheBunny(show, me, the, bunny)); // output: "show me the bunny"
函数showMeTheBunny(show,me,the,bunny)
{
for(设n=0;n 返回`${show}${me}${the}${bunny}:一个参数
对象的属性实际上是setter。如果您重新分配一个参数的属性,相应的变量名也会改变。因此您可以迭代参数
并重新分配它们:
//这些字符串中的每一个都用有意的和不必要的空格填充:
let show=“show”;
让我=“我”;
让“the”;
let bunny=“bunny”;
函数showMeTheBunny(show,me,the,bunny)
{
[…参数].forEach((arg,i)=>{
参数[i]=arg.trim();
});
返回`${show}${me}${the}bunny`;
}
log(showMeTheBunny(show,me,the,bunny));//输出:“show me the bunny”
您可以使用javascript的arguments对象!!
//这些字符串中的每一个都用有意的和不必要的空格填充:
let show=“show”;
让我=“我”;
让“the”;
let bunny=“bunny”;
函数showMeTheBunny(show,me,the,bunny)
{
var retString=“”;
for(var i=0;i
console.log(showMeTheBunny(show,me,the,bunny));//输出:“show me the bunny”
“假设我的函数有50个参数…”Youch!我不明白你所说的命名变量的意思,你能显示未命名变量吗?@LonnieBest:我明白了。通常这样的函数都有一个签名sqlExecute(语句,args)
,其中args
是一个数组(如果使用位置占位符),或者是一个对象(如果使用命名占位符)。@LonnieBest-True。您可以通过使用分解结构来解决这个问题。例如,假设您接受一个对象:For(const[name,value]of object.entries(args)){args[name]=value.trim();}
后跟const{show,me,the,bunny}=args;
。它的优点是您不必担心顺序。@LonnieBest:是的,但谁说您必须使用模板?用您自己的占位符传递一个普通字符串,如WHERE{WHERE}>0
并在运行中替换它们。模板在这里是一个错误的工具。不美观。你的兔子将有一个拖尾space@mplungjan它不会,看看返回语句:)现在它不会了。与join相比仍然不雅观值得注意的是,参数
在严格模式下不会以这种方式运行。返回到形式参数的链接被删除d、 @T.J.Crowder我从来没有使用过strict。但是它可以使用。你有什么顾虑吗?啊,不允许重写arguments@mplungjan-右。参数[0]=x
在严格模式下不会更改显示
。您的答案之所以有效,是因为您不使用形式参数。因此,如果我在ES6模块中执行此操作,它将自动处于严格模式。因此,在这种情况下它是否不起作用?@LonnieBest-正是如此。在这种情况下,您需要执行类似于我的第二或第三个选项的操作ad.@mplungjan-感谢您的编辑!我没有看到OP在问题中解决了这个问题。OP说他们想更改形式参数的值。但事实并非如此。@t.J.Crowder他最终这么说了(在我发布后,由您提示):)我们确定他真的是那个意思吗?我的意思是,重点是什么。不,是在。从最后两段,很清楚“命名变量”是什么意思。@T.J.Crowder对。我仍然不明白为什么这会是一件有用的事情。这仍然是一个有趣的答案。谢谢。