|
Initialization |
|
|
var's may be initialized optionally, if initialization is omitted they are initialized to an empty string: var x; // equivalent to var x = ''; var yMax = 12, message = 'hello world'; const initialization is mandatory (except for externals). When initializing an array with a single value, all elements are set to that value: var a1[100]; // all elements initialized to '' const a2[12] = 100; // all elements set to 100 Array elements may be initialized similar to in C/C++ supplying the values in braces. There is a difference between global array initialization and array initialization within functions. The latter is dynamic while globals are static. Example:
var adr[4][2] = {
{ 1, 'fred' },
{ 2, "john" },
{ 3 }
};
If adr is a global array it's size is truly 4 by 2 as noted in the indexes. Not explicitly initialized elements are set to an empty string so adr will in fact show up like:
1 'fred'
2 'john'
3 ''
'' ''
However if adr were a local var (declared within a function) the indexes are irrelevant; the number of dimensions and their size is determined by the data itself. That will be an array of 3 rows and 2 columns in this case:
1 'fred'
2 'john'
3 ''
Since the array indexes are irrelevant for initialized local arrays you may omit them at all:
var adr = {
{ 1, 'fred' },
{ 2, 'john' },
{ 3, }
};
|
||||||||
| Copyright © IBK Landquart | Last revision: 27.05.2002 | << Back Top Next >> |