function foo1(a,b){
console.log(arguments); //["oldValue","oldValue"]
var newArguments = foo2.apply(this,arguments);
for (var i=0;i<arguments.length;i++){
arguments[i] = newArguments[i];
}
console.log(arguments); //["newValue","newValue"]
}
function foo2(){
arguments[0] = "newValue";
arguments[1] = "newValue";
console.log(arguments); //["newValue","newValue"]
return arguments;
}
foo1("oldValue","oldValue");
我想通过外部函数foo2更改foo1参数值。我通过在foo2中用新参数返回数组并用foo1中的返回数组替换foo1参数来完成此操作。还有其他更优雅的方式吗?如何通过Function.prototype.apply()更改调用者函数的参数?
为什么选择投票? – iplus26
如果我直接接收参数但有一些属性设置为foo1(a,b),那么如果我引用它们(而参数[0],参数[1]返回新值),则a和b保持不变。 –
@Paweł查看更新。 – iplus26