更改通过函数签名命名的所有变量的值

时间:2019-11-05 08:23:24

标签: javascript ecmascript-next

假设我有一个包含50个参数的函数,并且需要修改在函数签名内创建的每个命名变量的值。

他是一个只有4个例子的示例,而不是50个参数:

// 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)
{
	// I want to trim each argument, without having to do this:
	show = show.trim();
	me = me.trim();
	the = the.trim();
	bunny = bunny.trim();
	// The above lines, within this function,
	// are the ones I want to replace with a loop (if possible)

	return `${show} ${me} ${the} ${bunny}: ?`; 
}
console.log(showMeTheBunny(show, me, the, bunny)); // output: "show me the bunny: ?"

arguments对象可以访问传递给函数的所有参数,但是它似乎没有提供一种方法来更改命名变量本身的值。

是否有可能通过一个修改了每个命名变量的函数(在函数签名中命名)运行该函数,然后在以后使用这些修改后的参数(使用相同的变量名)之前对每个变量进行修改?

4 个答案:

答案 0 :(得分:4)

您已经说过要修改“命名变量”的值,因此我假设您是指形式参数(showme等)

  

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}: ?`;
}

在这种情况下,arguments[0] = arguments[0].trim()更新show形式参数的值,arguments[1] = arguments[1].trim()更新me,依此类推。但仅在宽松模式下。在严格模式下,只会更新arguments[x],而不会更新形式参数;回到它的链接被删除。 (值得注意的是,严格模式是模块和class构造中的默认模式。)

实时示例:

// 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)
{
    for (let n = 0; n < arguments.length; ++n)
    {
        arguments[n] = arguments[n].trim();
    }
	return `${show} ${me} ${the} ${bunny}: ?`;
}
console.log(showMeTheBunny(show, me, the, bunny)); // output: "show me the bunny"

还有其他方法,但是它们不会修改形式参数的值。例如,您可以使用rest参数:

function showMeTheBunny(...rest)
{
    rest = rest.map(entry => entry.trim());
    const [show, me, the, bunny] = rest;
    return `${show} ${me} ${the} ${bunny}: ?`;
}

实时示例:

"use strict";
// 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(...rest)
{
    rest = rest.map(entry => entry.trim());
    const [show, me, the, bunny] = rest;
	return `${show} ${me} ${the} ${bunny}: ?`;
}
console.log(showMeTheBunny(show, me, the, bunny)); // output: "show me the bunny"

在严格模式下有效。

另一种选择是接受具有参数属性的对象,然后(再次)使用解构获得单个变量:

function showMeTheBunny(args)
{
    for (const [name, value] of Object.entries(args)) {
        args[name] = value.trim();
    }
    const {show, me, the, bunny} = args;
    return `${show} ${me} ${the} ${bunny}: ?`;
}

实时示例:

"use strict";
// 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(args)
{
    for (const [name, value] of Object.entries(args)) {
        args[name] = value.trim();
    }
    const {show, me, the, bunny} = args;
	return `${show} ${me} ${the} ${bunny}: ?`;
}
console.log(showMeTheBunny({show, me, the, bunny})); // output: "show me the bunny"

在严格模式下也可以。

答案 1 :(得分:3)

你是这个意思吗?

将迭代参数转换为数组并将其映射

Array.from(arguments)[...arguments]将在这里工作

这实际上并没有修改TJ指出的实际参数数组

// 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) {
  return Array.from(arguments).map(el => el.trim()).join(" ")+": ?";
}
console.log(showMeTheBunny(show, me, the, bunny)); // output: "show me the bunny"

答案 2 :(得分:3)

arguments对象的属性实际上是设置器。如果在参数上重新分配属性,则相应的变量名称也会更改。因此,您可以遍历arguments并重新分配它们:

// 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"

但这真的真的很奇怪。 Javascript中几乎没有其他东西表现出这种极其不直观的行为。考虑改换功能。

答案 3 :(得分:1)

您可以使用javascript的arguments对象! https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments

// 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)
{
    var retString = "";
	for (var i = 0; i < arguments.length; i++) {
       retString += arguments[i].trim() + " ";
    }

	return retString.trim(); 
}
console.log(showMeTheBunny(show, me, the, bunny)); // output: "show me the bunny"