这个问题已经在这里有了答案: > How do I empty an array in JavaScript? 18个
我正在使用ArrayList作为我的数组,
let ArrayList = ['a','b','c','d','e','f'];
我在方法1和方法2之间感到困惑,因为在两种情况下,我都引用了ArrayList,您也可以通过此链接查看日志https://jsfiddle.net/mnjsnj/u1fms8wx/2/
方法1
let Method1 = ArrayList; // Referenced arrayList by another variable
ArrayList= []; // Empty the array
console.log(Method1); // Output ['a','f']
方法二
let Method2 = ArrayList; // Referenced arrayList by another variable
ArrayList.length = 0; // Empty the array by setting length to 0
console.log(Method2 ); // Output []
最佳答案
ArrayList在第一个方法之后被清空,因此您正在向Method2分配一个空数组
let ArrayList = ['a','f'];
let Method1 = ArrayList; // Referenced arrayList by another variable
ArrayList = []; // Empty the array
console.log(Method1); // Output ['a','f']
console.log('ArrayList after Method1....!',ArrayList)
// here an empty array is assinged to Method2
let Method2 = ArrayList; // Referenced arrayList by another variable
ArrayList.length = 0; // Empty the array by setting length to 0
console.log(Method2); // Output []
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。