foo = []
creates a new array and assigns it a reference to the variable. Any other references are not affected and still point to the original array.
foo.length = 0
changes the array itself. If you access it through another variable, you still get a modified array.
I read somewhere that the second creates a new array, destroying all references to the existing array
This is back. Creates a new array and does not destroy other links.
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;
and it clears the array, at least for the first time. After the first time you do this:
arr = arr + $(this).html();
... which overwrites the array with a string.
The length
property of a string is read-only, so setting it to 0
has no effect.
Quentin Jan 26 '11 at 12:01 2011-01-26 12:01
source share