Reputation: 156434
This is a basic question about the JavaScript (ECMAScript) language so I apologize in advance if it's a duplicate (a little searching didn't reveal my exact question).
In ECMAScript we can use two basic syntactic forms to get/set properties on an object and they seem to have the exact same effect. Since I don't know better, I'll call them "property" and "associative array" notations:
var o = {};
// Property notation.
o.foo = 'Foo'; // (set)
o.foo; // => "Foo" (get)
// Associative array notation.
o['bar'] = 'Bar'; // (set)
o['bar']; // => "Bar" (get)
// They seem to be interchangeable.
o['foo']; // => "Foo"
o.bar; // => "Bar"
Are there any real differences between these two notations? Obviously, the associative array notation allows us to lookup dynamically generated keys on the object (and forcibly casts its argument to a string) whereas the property notation uses a literal but is that the only difference?
Upvotes: 3
Views: 618