Initialization |
|
var's may be initialized optionally, if initialization is ommitted 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 externs). 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 ar static. Example: var adr[4][2] = { { 1, 'fred' }, { 2, "john" }, { 3 } }; If adr is a global array it's size is truely 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 theire 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 ommit them at all: var adr = { { 1, 'fred' }, { 2, 'john' }, { 3, } }; | ||||||||
Copyright © IBK Landquart | Last revised by Peter Koch, 24.02.00 | << Back Top Next >> |