0

are objects with properties the equivalent of associative arrays?

is it ok if the property names have punctuation characters?

like obj['http://google.com'] = { ... } ?

i can still access it like an array - obj['http://google.com'], but not like an object because of the slashes and stuff.

basically i`m asking if it's considered a bad practice to use objects like this. if yes, is there any other alternative of getting associative arrays?

thelolcat
  • 10,995
  • 21
  • 60
  • 102
  • 3
    Yes, objects are sort of the same as associative arrays in languages like PHP. There are two ways to access any property in an object, dot notation or bracket notation. The latter is used if there are special characters in the key or if you need to use a variable, and has nothing to do with arrays. No, it's not considered bad practice to do this, it's the way objects work. – adeneo Dec 21 '13 at 21:25
  • ok tks for claryifing that;) – thelolcat Dec 21 '13 at 21:29

2 Answers2

2

are objects with properties the equivalent of associative arrays?

Not exactly, for they are not ordered. They're more like a Map.

is it ok if the property names have punctuation characters, like obj['http://google.com'] = { ... } ?

Yes. As you could see, it works :-)

i can still access it like an array - obj['http://google.com'], but not like an object because of the slashes and stuff.

That's nothing array-specific. If the property name (of any object) is not a valid identifier (because it's a number or does contain a dot) or a dynamic value (like a variable), you have to use bracket notation for property access. In object literals, you can quote the keys.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • The terms "associative array" and "Map" are often synonymous. The fact that *some* quarters have decided to make a distinction between the two does not make the distinction universal. – Louis Dec 21 '13 at 21:45
  • @Louis: Are they? OK. "Map" always seemed more general to me, while "associative *array*" indicated some ordering - at least in PHP where the term is most used. – Bergi Dec 21 '13 at 21:55
  • I see where you are coming from but usage varies from subculture to subculture. – Louis Dec 21 '13 at 21:57
2

There's a risk using objects as associative arrays in JavaScript. Consider:

var foo = {};

function putFoo(x, v) {
    foo[x] = v;
}

function getFoo(x) {
    return foo[x];
}

putFoo("a", "blah");
console.log("a", getFoo("a")); // Yes, "a" is there.
console.log("b", getFoo("b")); // No, "b" is not there.
console.log("toString", getFoo("toString")); // what?

Everything is swell until you get to the last console.log. toString is a method in Object.prototype from which object literals get their methods, so you'll get a function out on the console even though you never put anything named toString in the object.

You can work around this by creating your object with Object.create(null). This will create an object that does not have a prototype, and so is not polluted by the members of Object. (Documentation on Object.create.)

Here's a question that explores the performance issues. In brief creating an object with Object.create(null) is more expensive than with {} but accessing the keys on an object created by Object.create(null) is faster than on an object created with {}.

Community
  • 1
  • 1
Louis
  • 146,715
  • 28
  • 274
  • 320