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