contents   index   previous   next

Automatic and JavaScript Arrays


C style automatic arrays have just been discussed. Perhaps some simple and direct comparisons of the two different kinds of arrays would be helpful.


The following lines of code create an automatic array of 3 elements:


var a;

a[0] = 0;

a[1] = "one";

a[2] = 2;


The following line of code creates an automatic array consisting of objects that have information about files in the root directory of drive C. See Several functions from the C library objects, Clib and SElib, return automatic arrays.


var a ="c:\\*.*");


The following two lines of code both produce identical JavaScript arrays of 3 elements each.


var a = new Array(0, "one", 2);

var a = [0, "one", 2];


The following lines of code also produce a JavaScript array which is identical to the two immediately preceding arrays:


var a = new Array();

a[0] = 0;

a[1] = "one";

a[2] = 2;


The elements of automatic and JavaScript arrays are accessed in the same way using indices, for example:


a[3] = "three";



These lines behave the same for both automatic and JavaScript arrays. But what are some of the differences? The following fragment:


var aa;

aa[0] = 0;

aa[1] = "one";


var ja = [0, "one"];








results in the following display:







which shows that both automatic and JavaScript arrays are of type object. But automatic arrays belong to the Object class and JavaScript arrays belong to the Array class. See array.jsh - arrays and objects for more information on the differences. The Array class inherits the properties of the Object class but the Object class does not have the properties of the Array class. What does that mean?


Instances of both automatic arrays and JavaScript arrays may use the properties and methods of the Object class, but only JavaScript arrays may use the properties of the Array class. For example and using the two arrays defined immediately above, the length property of the Array class may only be used with the JavaScript array ja, that is, var len = ja.length is valid but var len = aa.length is an error. To get the length of aa, an automatic array, the function global.getArrayLength() must be used with it. As just explained, the JavaScript array ja may also be used with the function. That is, both of the following are valid: getArrrayLength(aa) and getArrrayLength(ja).


Having both types of arrays is a result of providing the C standard library in the Clib and SElib objects. If you want to simplify matters use the Object convert() method to convert arrays from one class to the other. In general, if you convert automatic arrays to JavaScript arrays and work only with JavaScript arrays, your scripting will be simpler and more powerful. But, remember, if you are only going to do simple things with arrays, then working with either class of array is quick and simple.


Literal strings