4 ms·
Do people actually use the object-as-an-associative-array property access as a general practice? I use it when it's useful, but if the name of the property sho
by scarmig 15y ago
Do people actually use the object-as-an-associative-array property access as a general practice?
I use it when it's useful, but if the name of the property should not be determined at run-time, I always do the dot notation. I've never heard of anyone taking issue with it, either.
- asolove 15y agoThe original quote is "When accessing object properties that are determined at run-time or which contain characters not compatible with dot notation, use square bracket notation." To which should be added: object properties that are reserved words in the language. The most common problem here is people using the dot notation for properties like "object.default" or "object.class" that are syntactically invalid. The rest of the article is a strange mix of good advice and some cases of pretty-bad code being presented as better than very-bad code.
- jnbiche 15y agoI agree completely. This article mixes obvious no-no's like using var and avoiding cluttering the global namespace with headscratchers like the one you mention. This article would more accurately be titled best practices for DOM interactions since most of the suggestions deal with DOM issues rather than pure language issues (and most of these are easily dealt with through the use of a good library like jquery). The Google Javascript style guide someone mentioned above is much more relevant to pure language issues, and is the one style guide I reference frequently (or should reference frequently).