V
VK
Or why I just did myArray['item01'] = "Computers" but myArray.length is
showing 0. What a hey?
There is a new trend to treat arrays and hashes as they were some
variations of the same thing. But they are not at all.
If you are doing *array", then you have to use only integer values for
array index, as it was since ALGOL.
Hash (Associative array) doesn't exists in JavaScript as a separate
programming entity. But each object inherits internal hash mechanics
from Object() constructor. In hash all keys are CDATA strings (even if
you provide a number for a key, internally it's sorted and treated as a
string).
Now some JavaScript specifics: as Array extends Object, it can be also
used as a hash. So you can do something like:
var arr = new Array();
// add to array, arr.length == 1
arr[0] = 10;
// add new property (key/value pair)
// arr.length is *not* affected !
arr['foo'] = 'JavaScript is funny sometimes';
Form values always returned to function as strings.
So in a situation like
arr[myForm.myField.value] = 4; // say myForm.myField.value == 17
JavaScript cannot determine what do you want from it: whether
you want to add new property called "17" or you want
to add an array element with index 17.
To work securely with *array* you should do something like:
var arr = new Array();
....
var i = Math.parseInteger(myForm.myField.value, 10);
if (i) {arr = quantity;}
This is with a value check.
To skip on value check you can do runtime typisation by prefixing value
with "+" (script will try to convert the following expression into a
number):
app[+myForm.myField.value] = quantity;
Then later:
for (i=0; i<arr.length; i++) {
// check arr
}
If you want to use hash (say using item names as your keys), you better
use the generic Object() constructor to have you hash free from
inherited properties.
var hash = new Object();
hash[key] = someValue;
Then later:
for (key in hash) {
// check hash[key]
showing 0. What a hey?
There is a new trend to treat arrays and hashes as they were some
variations of the same thing. But they are not at all.
If you are doing *array", then you have to use only integer values for
array index, as it was since ALGOL.
Hash (Associative array) doesn't exists in JavaScript as a separate
programming entity. But each object inherits internal hash mechanics
from Object() constructor. In hash all keys are CDATA strings (even if
you provide a number for a key, internally it's sorted and treated as a
string).
Now some JavaScript specifics: as Array extends Object, it can be also
used as a hash. So you can do something like:
var arr = new Array();
// add to array, arr.length == 1
arr[0] = 10;
// add new property (key/value pair)
// arr.length is *not* affected !
arr['foo'] = 'JavaScript is funny sometimes';
Form values always returned to function as strings.
So in a situation like
arr[myForm.myField.value] = 4; // say myForm.myField.value == 17
JavaScript cannot determine what do you want from it: whether
you want to add new property called "17" or you want
to add an array element with index 17.
To work securely with *array* you should do something like:
var arr = new Array();
....
var i = Math.parseInteger(myForm.myField.value, 10);
if (i) {arr = quantity;}
This is with a value check.
To skip on value check you can do runtime typisation by prefixing value
with "+" (script will try to convert the following expression into a
number):
app[+myForm.myField.value] = quantity;
Then later:
for (i=0; i<arr.length; i++) {
// check arr
}
If you want to use hash (say using item names as your keys), you better
use the generic Object() constructor to have you hash free from
inherited properties.
var hash = new Object();
hash[key] = someValue;
Then later:
for (key in hash) {
// check hash[key]