Question about arrays

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved

Join Date: Apr 2007
Posts: 25
Reputation: jmasta is an unknown quantity at this point 
Solved Threads: 0
jmasta jmasta is offline Offline
Light Poster

Question about arrays

 
0
  #1
Aug 18th, 2008
Hello everyone, I just have a quick Javascript question I haven't found an answer to on the net. What is the difference between
arrayName[0] and
arrayName.item(0) I've run across instances of both, but nowhere have I seen the difference between the two.
Thank you,
Jmasta
Last edited by jmasta; Aug 18th, 2008 at 1:50 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 954
Reputation: essential will become famous soon enough essential will become famous soon enough 
Solved Threads: 131
Featured Poster
essential's Avatar
essential essential is offline Offline
Posting Shark

Re: Question about arrays

 
0
  #2
Aug 18th, 2008
Current best-practice eschews the "new" keyword on Javascript primitives. If you want to create a new Array simply use brackets [] like this...



var myArray = [];


You don't need to tell Javascript how many items to size the Array for. Javascript will automatically increase the size of the Array as needed, as you add items into the Array. Creating an Array with brackets instead of with the "new" constructor avoids a bit of confusion where you want to initialize only one integer. For instance.




JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. var badArray = new Array(10); // Creates an empty Array that's sized for 10 elements.
  2.  
  3.  
  4. var goodArray= [10]; // Creates an Array with 10 as the first element.



As you can see these two lines do two very different things. If you had wanted to add more than one item then badArray would be initialized correctly since Javascript would then be smart enough to know that you were initializing the array instead of stating how many elements you wanted to add.
Since the new constructor is not necessary with Arrays and there's a slight chance of unintended results by using the new constructor, it's recommended you not use "new Array()" to create an Array.
Dev.Opera — FOLLOW THE STANDARDS, BREAK THE RULES...
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 849
Reputation: R0bb0b is on a distinguished road 
Solved Threads: 67
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Practically a Posting Shark

Re: Question about arrays

 
0
  #3
Aug 18th, 2008
Usually when you see something like this:
arrayName.item(0)

it is referring to something in the DOM, for example:
http://jacksleight.com/blog/2008/01/14/getelementsby/
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss

-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 25
Reputation: jmasta is an unknown quantity at this point 
Solved Threads: 0
jmasta jmasta is offline Offline
Light Poster

Re: Question about arrays

 
0
  #4
Aug 19th, 2008
I'm sorry, I should have been more clear. Let's say I already did something like
var pageInputs = getElemetsByTagName("input"); Now, i have a collection, pageInputs. So what's the difference between
console.log(pageInputs[0]) and
console.log(pageInputs.item(0))
I haven't had a chance to test them, but wouldn't they return the same thing? I just hadn't seen the '.item()' syntax before.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 381
Reputation: langsor is an unknown quantity at this point 
Solved Threads: 33
langsor langsor is offline Offline
Posting Whiz

Re: Question about arrays

 
0
  #5
Aug 19th, 2008
Originally Posted by R0bb0b View Post
Usually when you see something like this:
arrayName.item(0)

it is referring to something in the DOM, for example:
http://jacksleight.com/blog/2008/01/14/getelementsby/
R0bb0b is correct ... item(0) accesses a node-collection
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script type="text/javascript">
  2.  
  3. arr = [];
  4. arr[0] = 'one';
  5. arr['two'] = 3;
  6.  
  7. alert( arr[0] );
  8. alert( arr['two'] );
  9.  
  10. $links = document.getElementsByTagName('a');
  11. for ( var i = 0; i < links.length; i ++ ) {
  12. alert( links.item(i).href );
  13. }
  14. </script>

as an example of different arrays / node collections
Last edited by langsor; Aug 19th, 2008 at 1:46 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 381
Reputation: langsor is an unknown quantity at this point 
Solved Threads: 33
langsor langsor is offline Offline
Posting Whiz

Re: Question about arrays

 
0
  #6
Aug 19th, 2008
Originally Posted by jmasta View Post
I'm sorry, I should have been more clear. Let's say I already did something like
var pageInputs = getElemetsByTagName("input"); Now, i have a collection, pageInputs. So what's the difference between
console.log(pageInputs[0]) and
console.log(pageInputs.item(0))
I haven't had a chance to test them, but wouldn't they return the same thing? I just hadn't seen the '.item()' syntax before.
Yes, they would return the same thing and are effectively the same, but the .item(0) is the correct syntax for DOM node-collections, but the array access syntax is cross-browser supported.

...
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 25
Reputation: jmasta is an unknown quantity at this point 
Solved Threads: 0
jmasta jmasta is offline Offline
Light Poster

Re: Question about arrays

 
0
  #7
Aug 19th, 2008
Thank you folks, and thanks Langsor. That's what I was thinking, but just wanted to make sure.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 748 | Replies: 6
Thread Tools Search this Thread



Tag cloud for JavaScript / DHTML / AJAX
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC