更改通过函数签名命名的所有变量的值
Change Values of All Variables Named via Function Signature
假设我有一个有 50 个参数的函数,我需要修改在函数签名中创建的每个命名变量的值。
而不是 50 个参数,他是一个只有 4 个参数的例子:
// 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 对象可以访问传递给函数的所有参数,但它似乎没有提供一种方法来更改命名变量本身的值。
是否可以 运行 通过一个函数修改所有命名变量(在函数签名中命名),然后在以后使用这些修改后的参数(使用相同的变量名)之前修改它们中的每一个?
你是这个意思?
将迭代参数转换为数组并映射它
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"
您说过要修改 "named variables" 的值,所以我假设您指的是形式参数(show
、me
等)
The arguments object can access all the arguments passed into a function, but it doesn't seem to offer a way to change the values of the named-variables themselves.
确实如此,但仅限于宽松模式,而非严格模式:
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]
,不更新形参; link 回到它被删除。 (值得注意的是,严格模式是模块和 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"
还有其他方法,但不会修改形参的值。例如,您可以使用休息参数:
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"
这也适用于严格模式。
arguments
对象的属性实际上是 setter。如果您在参数上重新分配 属性 ,相应的变量名称也会更改。因此,您可以遍历 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 中几乎没有其他东西表现出这种极其不直观的行为。考虑改变你的功能。
可以使用javascript的参数对象!!
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"
假设我有一个有 50 个参数的函数,我需要修改在函数签名中创建的每个命名变量的值。
而不是 50 个参数,他是一个只有 4 个参数的例子:
// 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 对象可以访问传递给函数的所有参数,但它似乎没有提供一种方法来更改命名变量本身的值。
是否可以 运行 通过一个函数修改所有命名变量(在函数签名中命名),然后在以后使用这些修改后的参数(使用相同的变量名)之前修改它们中的每一个?
你是这个意思?
将迭代参数转换为数组并映射它
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"
您说过要修改 "named variables" 的值,所以我假设您指的是形式参数(show
、me
等)
The arguments object can access all the arguments passed into a function, but it doesn't seem to offer a way to change the values of the named-variables themselves.
确实如此,但仅限于宽松模式,而非严格模式:
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]
,不更新形参; link 回到它被删除。 (值得注意的是,严格模式是模块和 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"
还有其他方法,但不会修改形参的值。例如,您可以使用休息参数:
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"
这也适用于严格模式。
arguments
对象的属性实际上是 setter。如果您在参数上重新分配 属性 ,相应的变量名称也会更改。因此,您可以遍历 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 中几乎没有其他东西表现出这种极其不直观的行为。考虑改变你的功能。
可以使用javascript的参数对象!! 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"