Welcome to WebmasterWorld Guest from 23.20.120.3

Forum Moderators: open

Message Too Old, No Replies

arrays

object versus enumerated array

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

Junior Member

5+ Year Member

joined:Feb 7, 2010
posts: 119
votes: 0


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?
3:35 pm on Oct 5, 2011 (gmt 0)

Senior Member

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

joined:July 3, 2006
posts: 3123
votes: 0


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.
4:08 pm on Oct 5, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Feb 7, 2010
posts: 119
votes: 0


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!
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members