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");
foo1引数の値を外部関数foo2で変更したいと思います。私はfoo2の新しい引数で配列を返し、foo1の引数をfoo1の返された配列に置き換えることでそれを行いました。それ以外の、よりエレガントな方法がありますか?Function.prototype.apply()で呼び出し元関数の引数を変更する方法は?
なぜダウン票ですか? – iplus26
引数を直接受け取ったにもかかわらず、このfoo1(a、b)のようないくつかのプロパティを設定した場合、aとbは参照しても変わりません(arguments [0]、arguments [1]は新しい値を返します)。 –
@Pawełアップデートをチェックしてください。 – iplus26