homepage Welcome to WebmasterWorld Guest from 54.166.100.8
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
arrays
object versus enumerated array
nyteshade




msg:4371038
 1:44 pm on Oct 5, 2011 (gmt 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?

 

penders




msg:4371092
 3:35 pm on Oct 5, 2011 (gmt 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.

nyteshade




msg:4371115
 4:08 pm on Oct 5, 2011 (gmt 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!
Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved