Forum Moderators: open
function displayAbbreviations(){
var abbreviations = document.getElementsByTagName("abbr");
if(abbreviations.length<1) return false;
var defs = new Array();//<<<<----INITIALLY
for(var i=0; i<abbreviations; i++){
var definition = document.abbreviations[i].getAttribute("title");
var key = abbreviations[i].lastChild.nodeValue;
defs[key]=definition; //<<<<----HERE
}
}
My understanding is that a js array is a variable that encapsulates a collection of variables as an enumerated array, AND, it is an object, yes?
So that if I did 'myvar=[]' then I just created an array indexed by numbers, correct?
So, my lesson example above HERE, creates an enumerated array INITIALLY and then assigns the index a text key HERE?!?!?
Doesn't this create an associative array? And, if yes, then would it have been more comprehending to create 'var def = {}' INITIALLY?
When assigning an index that is actually a string, you are creating a property in the array object. You are not adding an item of data to the array - which are all indexed by positive numeric values. If you later examine defs.length, for instance, it will return 0. Other array methods such as join() etc. will also not return anything - since the array is empty.This is the key to understanding js arrays; I've read this before but it's been hard to keep it in (my) mind... objects! I do believe in objects, I do I do!