Javascript Array.length = 0 和 Array =[] 的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4804235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:10:59  来源:igfitidea点击:

Difference between Array.length = 0 and Array =[]?

javascriptarrays

提问by Srikanth Rayabhagi

Can some one explain the conceptual difference between both of them. Read somewhere that the second one creates a new array by destroying all references to the existing array and the .length=0 just empties the array. But it didn't work in my case

有人可以解释一下两者之间的概念差异吗?在某处读到第二个通过销毁对现有数组的所有引用来创建一个新数组,而 .length=0 只是清空数组。但它在我的情况下不起作用

//Declaration 
var arr = new Array();

The below one is the looping code that executes again and again.

下面是一次又一次执行的循环代码。

$("#dummy").load("something.php",function(){
   arr.length =0;// expected to empty the array
   $("div").each(function(){
       arr = arr + $(this).html();
   });
});

But if I replace the code with arr =[]in place of arr.length=0it works fine. Can anyone explain what's happening here.

但是,如果我用替换代码arr =[]代替arr.length=0它工作正常。谁能解释这里发生了什么。

回答by Quentin

foo = []creates a new array and assigns a reference to it to a variable. Any other references are unaffected and still point to the original array.

foo = []创建一个新数组并将对它的引用分配给一个变量。任何其他引用不受影响,仍指向原始数组。

foo.length = 0modifies the array itself. If you access it via a different variable, then you still get the modified array.

foo.length = 0修改数组本身。如果您通过不同的变量访问它,那么您仍然会得到修改后的数组。

Read somewhere that the second one creates a new array by destroying all references to the existing array

在某处读到第二个通过销毁对现有数组的所有引用来创建一个新数组

That is backwards. It creates a new array and doesn't destroy other references.

那是倒退。它创建一个新数组并且不会破坏其他引用。

var foo = [1,2,3];
var bar = [1,2,3];
var foo2 = foo;
var bar2 = bar;
foo = [];
bar.length = 0;
console.log(foo, bar, foo2, bar2);

gives:

给出:

[] [] [1, 2, 3] []


arr.length =0;// expected to empty the array
arr.length =0;// expected to empty the array

and it does empty the array, at least the first time. After the first time you do this:

它确实清空了数组,至少是第一次。第一次执行此操作后:

arr = arr + $(this).html();
arr = arr + $(this).html();

… which overwrites the array with a string.

...用字符串覆盖数组。

The lengthproperty of a string is read-only, so assigning 0to it has no effect.

length字符串的属性是只读的,因此分配0给它不起作用。

回答by Matt

The difference here is best demonstrated in the following example:

此处的差异在以下示例中得到了最好的展示:

var arrayA = [1,2,3,4,5];

function clearUsingLength (ar) {
    ar.length = 0;
}

function clearByOverwriting(ar) {
    ar = [];
}

alert("Original Length: " + arrayA.length);
clearByOverwriting(arrayA);
alert("After Overwriting: " + arrayA.length);
clearUsingLength(arrayA);
alert("After Using Length: " + arrayA.length);

Of which a live demo can be seen here: http://www.jsfiddle.net/8Yn7e/

其中一个现场演示可以在这里看到:http: //www.jsfiddle.net/8Yn7e/

When you set a variable that points to an existing array to point to a new array, all you are doing is breaking the link the variable has to that original array.

当您将指向现有数组的变量设置为指向新数组时,您所做的就是断开变量与原始数组的链接。

When you use array.length = 0(and other methods like array.splice(0, array.length)for instance), you are actuallyemptying the original array.

当您使用array.length = 0(以及其他方法array.splice(0, array.length),例如)时,您实际上是在清空原始数组。

回答by Denilson Sá Maia

Are you sure it really works?

你确定真的有效吗?

I did a little experiment here, and trying to "add" an Array with a String resulted in a string.

我在这里做了一个小实验,尝试用字符串“添加”一个数组,结果是一个字符串。

function xyz(){
    var a = [];
    alert(typeof(a+$("#first").html()));
    // shows "string"
}

http://www.jsfiddle.net/4nKCF/

http://www.jsfiddle.net/4nKCF/

(tested in Opera 11)

(在 Opera 11 中测试)