javascript - Difference between Array.length = 0 and Array =[]? -
can 1 explain conceptual difference between both of them. read somewhere second 1 creates new array destroying references existing array , .length=0 empties array. didn't work in case
//declaration var arr = new array();
the below 1 looping code executes again , again.
$("#dummy").load("something.php",function(){ arr.length =0;// expected empty array $("div").each(function(){ arr = arr + $(this).html(); }); });
but if replace code arr =[]
in place of arr.length=0
works fine. can explain what's happening here.
foo = []
creates new array , assigns reference variable. other references unaffected , still point original array.
foo.length = 0
modifies array itself. if access via different variable, still modified array.
read somewhere second 1 creates new array destroying references existing array
that backwards. creates new array , 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] []
Comments
Post a Comment