Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #23802

Re: Does an array have to be reinitialised when one precreate space, if so how?

Newsgroups comp.lang.javascript
Subject Re: Does an array have to be reinitialised when one precreate space, if so how?
From "Evertjan." <exxjxw.hannivoort@inter.nl.net>
References <cb9b8034-af8f-4663-b768-70a6ea1fb9c1@googlegroups.com> <deb4cb27-ed25-4641-b346-3af04cfd5f38@googlegroups.com>
Date 2014-04-06 09:27 +0200
Message-ID <XnsA3076028EC637eejj99@194.109.133.133> (permalink)

Show all headers | View raw


jonas.thornvall@gmail.com wrote on 05 apr 2014 in comp.lang.javascript:

> Ok it is possible to add  index in the positive direction of array
> however extending the array in the negative direction do not work at
> all. Kind of weird, but on the other side someone told me negative
> indexes should not work at all? 

A javascript array always starts with index [0] and has a "length" of the 
highest set member.

Members that are not set below the top one do not exist and take no memory,
when 'asked' they return 'undefined', which they really are.

You cannot "extend an array in the negative direction" meaning that the 
index would go below [0], but you can unshift() [= shift-right] the whole 
array up while adding a new values for for the inserted indexes:

=============================
var a = [];

alert(a.length); // 0, no members being defined yet

a[0] = 'x';
a[12345] = 'y';

alert(a.length); // 12346, being from [0] to [12345]

/* free after w3schools:
The unshift() method adds new members to the beginning of an array, 
[and returns the new length].
Note: This method changes the length of an array.
Tip: To add new members at the end of an array, use the push() method.
*/

var lng = a.unshift('z');

alert('length now: ' + lng); // length now: 12347
alert(a.length); // 12347, being from [0] to [12346]

alert(a[0]); // z
alert(a[1]); // x
alert(a[2]); // undefined
alert(a[12345]); // undefined 
alert(a[12346]); // y

/* WARNING:
An array is also an object,
so you can add properties to the object.
Those properites do not affect the array behavour,
like .length and .unshift().
These added properties are just aquired properties, 
not members of that array.
*/

a['-3'] = 'qqq';
a['one'] = 'one';

alert(a.length); // 12347

/* however, 
a positive integer string is converted to a number index:
*/

a['12999'] = 'rrr';

alert(a.length); // 13000

=============================


-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Does an array have to be reinitialised when one precreate space, if so how? jonas.thornvall@gmail.com - 2014-04-05 14:11 -0700
  Re: Does an array have to be reinitialised when one precreate space, if so how? jonas.thornvall@gmail.com - 2014-04-05 14:12 -0700
  Re: Does an array have to be reinitialised when one precreate space, if so how? jonas.thornvall@gmail.com - 2014-04-05 14:20 -0700
    Re: Does an array have to be reinitialised when one precreate space, if so how? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-04-06 09:27 +0200
      Re: Does an array have to be reinitialised when one precreate space, if so how? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2014-04-07 17:30 -0700
        Re: Does an array have to be reinitialised when one precreate space, if so how? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2014-04-07 23:34 -0700
        Re: Does an array have to be reinitialised when one precreate space, if so how? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-04-08 22:17 +0200
          Re: Does an array have to be reinitialised when one precreate space, if so how? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2014-04-08 17:01 -0700
            Re: Does an array have to be reinitialised when one precreate space, if so how? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-04-09 09:22 +0200
  Re: Does an array have to be reinitialised when one precreate space, if so how? jonas.thornvall@gmail.com - 2014-04-05 14:21 -0700
  Re: Does an array have to be reinitialised when one precreate space, if so how? jonas.thornvall@gmail.com - 2014-04-05 14:24 -0700

csiph-web