我对数组在传递到函数时如何处理有些困惑。我的问题是为什么 sample2.js 的输出结果不是 null?
// sample1.js======================================
// This clearly demonstrates that the array was passed by reference
function foo(o) {
o[1] = 100;
}
var myArray = [1,2,3];
console.log(myArray); // o/p : [1,2,3]
foo(myArray);
console.log(myArray); // o/p : [1,100,3]
//sample2.js =====================================
// upon return from bar, myArray2 is not set to null.. why so
function bar(o) {
o = null;
}
var myArray2 = [1,2,3];
console.log(myArray2); // o/p : [1,2,3]
bar(myArray2);
console.log(myArray2); // o/p : [1,100,3]