{"id":126,"date":"2010-07-12T12:38:39","date_gmt":"2010-07-12T10:38:39","guid":{"rendered":"http:\/\/www.tapper-ware.net\/blog\/?p=126"},"modified":"2010-07-12T12:38:39","modified_gmt":"2010-07-12T10:38:39","slug":"javascript-object-notation-is-a-strange-beast","status":"publish","type":"post","link":"https:\/\/www.tapper-ware.net\/blog\/javascript-object-notation-is-a-strange-beast\/","title":{"rendered":"Javascript Object Notation is a strange beast"},"content":{"rendered":"<p>I&#8217;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.<\/p>\n<p>Take his for example:<\/p>\n<p><code><a href=\"javascript:{title:'Hello World'}['title'];\" target=\"_new\">{title:'Hello World'}['title'];<\/a><\/code><\/p>\n<p>Looks an awful lot like an object definition with an immediate access to the title property, doesn&#8217;t it? Well try to run it.<\/p>\n<p>Huh? &#8220;title&#8221;? The problem is that this statement is ambiguous: It could either mean &#8220;Create object title set to Hello World&#8221; or &#8220;Code block with label title and string Hello World&#8221;. The meaning of [&#8216;title&#8217;] then would mean &#8220;Access title property&#8221; for the first interpretation and &#8220;Create new Array with string title&#8221;. In this case, the second meaning is chosen and [&#8216;title&#8217;] turns into the string &#8220;title&#8221; during output.<\/p>\n<p>Now, this doesn&#8217;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.<\/p>\n<p>What do we learn from this? Don&#8217;t forget to quote the identifiers, don&#8217;t hold back on brackets and don&#8217;t forget the semicolon:<\/p>\n<p><code><a href=\"javascript:({'title':'Hello World'})['title'];\" target=\"_new\">({'title':'Hello World'})['title'];<\/a><\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;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 &hellip; <a href=\"https:\/\/www.tapper-ware.net\/blog\/javascript-object-notation-is-a-strange-beast\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Javascript Object Notation is a strange beast<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.tapper-ware.net\/blog\/wp-json\/wp\/v2\/posts\/126"}],"collection":[{"href":"https:\/\/www.tapper-ware.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tapper-ware.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tapper-ware.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tapper-ware.net\/blog\/wp-json\/wp\/v2\/comments?post=126"}],"version-history":[{"count":7,"href":"https:\/\/www.tapper-ware.net\/blog\/wp-json\/wp\/v2\/posts\/126\/revisions"}],"predecessor-version":[{"id":182,"href":"https:\/\/www.tapper-ware.net\/blog\/wp-json\/wp\/v2\/posts\/126\/revisions\/182"}],"wp:attachment":[{"href":"https:\/\/www.tapper-ware.net\/blog\/wp-json\/wp\/v2\/media?parent=126"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tapper-ware.net\/blog\/wp-json\/wp\/v2\/categories?post=126"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tapper-ware.net\/blog\/wp-json\/wp\/v2\/tags?post=126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}