我不希望foo在這個例子中改變,但是當我在函數中將它修改爲數組時,它會改變參數。注意foo不會被function1改變...我猜是因爲它不直接修改參數?任何幫助避免這將不勝感激。爲什麼我的函數在修改數組時改變參數?
var foo = [1,2,3];
bar = function1(foo);
bar = function2(foo);
bar = function3(foo);
function function1(newFoo){
newFoo = [newFoo,'a',1];
return newFoo;
} //foo after function1 = 1,2,3
function function2(newFoo){
newFoo[0] = 'a';
return newFoo;
} //foo after function2 = a,2,3
function function3(newFoo){
newFoo.push('4');
return newFoo;
} //foo after function3 = a,2,3,4