
{"id":2327,"date":"2017-02-20T17:16:00","date_gmt":"2017-02-20T22:16:00","guid":{"rendered":"http:\/\/www.ikriv.com\/blog\/?p=2327"},"modified":"2017-02-20T17:16:22","modified_gmt":"2017-02-20T22:16:22","slug":"javascript-wtf-1","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=2327","title":{"rendered":"JavaScript WTF #1"},"content":{"rendered":"<p>In ECMA Script 6\u00a0<em>toString()<\/em> function can throw an exception. This makes generating debug output a treacherous minefield. In all other languages I know <i>toString()<\/i> on built-in objects is safe, and custom <i>toString()<\/i> implementations that throw an exception are not welcome.<\/p>\n<p>Consider the following code:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nfunction *generator() { yield 42; }\r\nvar it = generator();\r\nconsole.log(it.constructor); \/\/ Prints &quot;GeneratorFunction {}&quot;\r\nconsole.log(it.constructor.toString()); \/\/ TypeError: Function.prototype.toString is not generic\r\n<\/pre>\n<p>So, it actually <i>is<\/i> possible to print the object to the debug output, but only by itself, no manipulation is allowed. The error message is also quite cryptic: what do they exactly mean by &#8216;not generic&#8217; and why should I care?<\/p>\n<p>This may seem like a minor annoyance, but I believe it is so brutally violates the rule of least astonishment, that it proudly occupies #1 place among other JavaScript WTFs. In sane languages <i>toString()<\/i> should not normally fail, at least not for built-in classes. It may, of course, fail with something like <i>OutOfMemoryException<\/i>, but that&#8217;s not what most people would call &#8220;normally&#8221;. If <i>toString()<\/i> can throw, I am forced to write a try\/catch around every <i>toString()<\/i> when dealing with unknown objects, and this is, frankly, less than ideal. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>In ECMA Script 6\u00a0toString() function can throw an exception. This makes generating debug output a treacherous minefield. In all other languages I know toString() on built-in objects is safe, and <a href=\"https:\/\/ikriv.com\/blog\/?p=2327\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[4,15],"tags":[],"class_list":["entry","author-ikriv","post-2327","post","type-post","status-publish","format-standard","category-hack","category-webdev"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2327","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2327"}],"version-history":[{"count":3,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2327\/revisions"}],"predecessor-version":[{"id":2330,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2327\/revisions\/2330"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2327"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2327"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2327"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}