From: MikeJ on
I tried GlobalArray.length = 0;
and it worked... but is it legit?

Thanks
Mike

On Tue, 27 Apr 2010 14:17:58 -0700 (PDT), Sean Kinsey
<okinsey(a)gmail.com> wrote:

>On Apr 27, 11:05�pm, MikeJ <no_spam_ple...(a)nothere.com> wrote:
>> Need to know how to clear a global array so Array.length will = 0
>> (meaning no indexes). Cant find the information in the book.
>> Thanks
>> Mike
>
>If the Array is only referenced by a single global variable then the
>easiest way is to just replace the reference to with that of a new
>Array
>var globalArray = [foo, bar];
>
>//reset
>globalArray = [];
>
>Alternatively you can use splice which adds/removes elements from the
>array
>globalArray.splice(0, globalArray.length - 1);
>(not sure if '- 1' is needed here)

From: MikeJ on
Posted before I re-checked replies..
Thanks
Mike


On Tue, 27 Apr 2010 23:22:27 +0200, Stefan Weiss
<krewecherl(a)gmail.com> wrote:

>On 27/04/10 23:05, MikeJ wrote:
>> Need to know how to clear a global array so Array.length will = 0
>> (meaning no indexes). Cant find the information in the book.
>
>To truncate an array so that arr.length = 0, you need to write
>
> arr.length = 0;
>
>I know, weird and unexpected syntax, but there it is ;)

From: Sean Kinsey on
On Apr 27, 11:27 pm, Stefan Weiss <krewech...(a)gmail.com> wrote:
> On 27/04/10 23:17, Sean Kinsey wrote:
>
> > On Apr 27, 11:05 pm, MikeJ <no_spam_ple...(a)nothere.com> wrote:
> >> Need to know how to clear a global array so Array.length will = 0
> >> (meaning no indexes). Cant find the information in the book.
> >> Thanks
> >> Mike
>
> > If the Array is only referenced by a single global variable then the
> > easiest way is to just replace the reference to with that of a new
> > Array
> > var globalArray = [foo, bar];
>
> > //reset
> > globalArray = [];
>
> Strictly speaking, that doesn't "clear" the array, it just assigns the a
> new empty Array to the variable globalArray:
>
> var arr = [1, 2];
> var alias = arr;
>
> arr = [];
> print alias.length;  // 2 -- whoops!

Maybe you should read my statement again, especially the part with
"..is only referenced by a single global variable.."
From: Stefan Weiss on
On 28/04/10 00:11, Sean Kinsey wrote:
> On Apr 27, 11:27 pm, Stefan Weiss <krewech...(a)gmail.com> wrote:
>> Strictly speaking, that doesn't "clear" the array, it just assigns the a
>> new empty Array to the variable globalArray:
>>
>> var arr = [1, 2];
>> var alias = arr;
>>
>> arr = [];
>> print alias.length; // 2 -- whoops!
>
> Maybe you should read my statement again, especially the part with
> "..is only referenced by a single global variable.."

My example was just an attempt to illustrate the conceptual difference
between clearing an array (see subject) and replacing it with a new one.
If there's only a single reference, there won't be any practical
difference, of course. In that context, I would tend to prefer
'arr = []', too.


--
stefan
From: Richard Cornford on
MikeJ wrote:
> Sean Kinsey wrote:

> I tried GlobalArray.length = 0;
> and it worked... but is it legit?
<snip - misplaced, and redundant quote>

Yes it is legitimate. Array instances have a special (array specific)
version of the internal [[Put]] method that may modify the array's -
length - property if the property name to which a value is assigned
qualifies as an 'array index' and may delete 'array index' properties of
the array object when values are assigned to the array's - length -
property. In the latter case, when a value is assigned to - length - any
'array index' properties that are greater than or equal to the value
assigned are deleted. Thus assign zero to - length - and all 'array
index' properties of the array object are deleted.

(This special [[Put]] behaviour maintains/enforces the relationship
between the array's - length - property and its 'array index' properties
such that the - length - is always at least one greater than the largest
'array index' property.)

Richard.