我有一个函数可以重新排列另一个函数的参数以满足标准定义。
function main(a, b, c) {
console.log(a, b, c); // 1 undefined undefined
console.log(arguments[0], arguments[1], arguments[2]); // 1 undefined undefined
shiftArgs(arguments, 3); // 3 because I'm expecting 3 arguments.
console.log(arguments[0], arguments[1], arguments[2]); // null null 1
console.log(a, b, c); // 1 undefined undefined ***
}
function shiftArgs(args, c) {var i, len;
len = args.length;
if (len < c) {
for (i = c - 1; i >= 0; i -= 1) {
args[i] = ((i - c + len) > -1 ? args[i - c + len] : null);
}
args.length = c;
}
};
main(1); // only calling main with one argument, which therefore needs to be the last one.
*** 是问题行,应该是“null null 1”以匹配重新分配的参数对象。
arguments 对象根据我的需要进行更改,main 调用的值“1”移动到最后一个参数。但是,映射到参数的变量名称在我移动参数对象后不会更改(请参阅最后一个标有 *** 的 console.log)。这需要为 null null 1 以匹配更改的参数对象)。
如何让 shiftArgs 函数重新分配变量 a、b 和 c 以匹配参数对象?