I’m never quite sure whether the Javascript Object Notation is a blessing or a curse. Of course, it does allow for quick creation of lists, but the way it is implemented I always find myself adding a few extra brackets just to make sure it is really treated as an object definition, not a code block.
Take his for example:
{title:'Hello World'}['title'];
Looks an awful lot like an object definition with an immediate access to the title property, doesn’t it? Well try to run it.
Huh? “title”? The problem is that this statement is ambiguous: It could either mean “Create object title set to Hello World” or “Code block with label title and string Hello World”. The meaning of [‘title’] then would mean “Access title property” for the first interpretation and “Create new Array with string title”. In this case, the second meaning is chosen and [‘title’] turns into the string “title” during output.
Now, this doesn’t happen nearly as often outside javascript: urls since the code grammar usually dictates how to interpret the block, but it still occasionally pops up.
What do we learn from this? Don’t forget to quote the identifiers, don’t hold back on brackets and don’t forget the semicolon: