It shall be done
- JavaScript and associative arrays don’t mix In WebTech, 392 days ago
-
The concept of “associative arrays” is an alien, in the JavaScript world.
I hear you “No, I’ve used them and they work”. My answer is “No, you have MISused something that looks like an associative array”.
Let me explain. When you write something like…
var friend = []; friend['name']='Claudio'; friend['age']='40';
…you’re creating an empty array (friend), and then you’re extending the friend object adding two properties (“name” and “surname”) to it. You were telling JavaScript that you’ll going to use an object with an indexed access (the array…), but then you use it as a simple object. Not fair.
You’d probably want to create something like this:
var friend = {}; // or new Object(); friend['name']='Claudio'; friend['age']='40';Your “associative array” is now a semantically correct JavaScript object.
Let’s look at a final example:
var friend = {}; // or new Object(); friend['name']='Claudio'; friend['age']='40'; // Properties can also be accessed with the dot notation friend.name='Claudio'; friend.age='40'; // When using arrays, you're using something that is numerically indexed var friend = []; // *or new Array();* friend[0]='Claudio'; friend[1]='40'; // See? :)HTH
Comments
commenting closed for this article
→ 61648624
→

