
{"id":2167,"date":"2016-12-20T11:12:26","date_gmt":"2016-12-20T16:12:26","guid":{"rendered":"http:\/\/www.ikriv.com\/blog\/?p=2167"},"modified":"2016-12-20T11:45:17","modified_gmt":"2016-12-20T16:45:17","slug":"javascript-arrow-function-wtf","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=2167","title":{"rendered":"Javascript arrow function WTF"},"content":{"rendered":"<p>I thought that JavaScript arrow functions are just a shorthand notation for regular functions. I was wrong, and I found it the hard way. Arrow function treat &#8216;this&#8217; keyword differently. <\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar x = 3, y = 4;\r\nvar obj = { \r\n  x: 42, \r\n  y: 50, \r\n  length1: function() { return Math.sqrt(this.x*this.x+this.y*this.y); }, \/\/ this === obj\r\n  length2: () =&gt;               Math.sqrt(this.x*this.x+this.y*this.y),    \/\/ this === global\r\n};\r\n\r\nconsole.log('length1: ' + obj.length1());\r\nconsole.log('length2: ' + obj.length2());\r\n<\/pre>\n<p>They <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Functions\/Arrow_functions\">explain it<\/a> by stating that separate &#8216;this&#8217; scope for callback functions was getting in the way, so they got rid of it for arrow functions. RTFM, RTFM, and once more RTFM.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I thought that JavaScript arrow functions are just a shorthand notation for regular functions. I was wrong, and I found it the hard way. Arrow function treat &#8216;this&#8217; keyword differently. <a href=\"https:\/\/ikriv.com\/blog\/?p=2167\" 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-2167","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\/2167","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=2167"}],"version-history":[{"count":6,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2167\/revisions"}],"predecessor-version":[{"id":2173,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2167\/revisions\/2173"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2167"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2167"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}