数组被复制了吗?
重要性:3
这段代码会显示什么?
let fruits = ["Apples", "Pear", "Orange"];
// push a new value into the "copy"
let shoppingCart = fruits;
shoppingCart.push("Banana");
// what's in fruits?
alert( fruits.length ); // ?
结果是 4
let fruits = ["Apples", "Pear", "Orange"];
let shoppingCart = fruits;
shoppingCart.push("Banana");
alert( fruits.length ); // 4
这是因为数组是对象。因此,shoppingCart
和 fruits
都是指向同一个数组的引用。