Welcome to WebmasterWorld Guest from 54.196.224.166

Forum Moderators: open

Message Too Old, No Replies

arrays

object versus enumerated array

     

nyteshade

1:44 pm on Oct 5, 2011 (gmt 0)

5+ Year Member



I'm back to DOM core scripting and js arrays and halfway through DOM Scripting, 2nd edition. So, here's a lesson I'm building:

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?, which I can access by 'myvar[0]' an enumerated array collection, AND like, 'myvar.length', an object, a collection of methods and properties. 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?

penders

3:35 pm on Oct 5, 2011 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Basically... yes! :)

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?


An array is a particular type of object that contains additional methods for manipulating the array. The data values in an array are indexed by positive numbers.

So that if I did 'myvar=[]' then I just created an array indexed by numbers, correct?


Yes.

So, my lesson example above HERE, creates an enumerated array INITIALLY and then assigns the index a text key HERE?!?!?


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.

Doesn't this create an associative array? And, if yes, then would it have been more comprehending to create 'var def = {}' INITIALLY?


Well, yes. To create an associative array you should declare it initially as an object. Either as an object literal "{}" or with "new Object()". Trying to use an Array as an associative array could get you in trouble. Some JS libraries add additional methods to the Array() object and these could break your script.

nyteshade

4:08 pm on Oct 5, 2011 (gmt 0)

5+ Year Member



penders:
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!