【发布时间】:2019-07-14 03:08:21
【问题描述】:
当我分配 anotherArrayList = arrayList 时,anotherArrayList 是指向 arrayList 的指针。那么为什么当我清空 arrayList 时这不起作用。
var arrayList = ['a', 'b', 'c', 'd', 'e', 'f'];
var anotherArrayList = arrayList;
arrayList[1] = '15';
console.log(arrayList); // output: [ "a", "15", "c", "d", "e", "f" ]
console.log(anotherArrayList); // output: [ "a", "15", "c", "d", "e", "f" ]
arrayList = [];
console.log(arrayList); // output: []
console.log(anotherArrayList); // output: [ "a", "15", "c", "d", "e", "f" ]
【问题讨论】:
-
当您执行
arrayList = [];时,您不会更改arrayList指向的对象;您正在使arrayList指向一个新对象。你的前提是错误的:“我清空了arrayList”不是正在发生的事情。 -
@ChrisG 这应该是一个答案。
-
这能回答你的问题吗? Pass-by-reference JavaScript objects
标签: javascript