{"id":112,"date":"2016-09-03T23:29:26","date_gmt":"2016-09-03T23:29:26","guid":{"rendered":"http:\/\/www.bhoomabrsr.com\/blog\/?p=112"},"modified":"2016-09-03T23:29:26","modified_gmt":"2016-09-03T23:29:26","slug":"arrow-functions-environment-remembering","status":"publish","type":"post","link":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/2016\/09\/03\/arrow-functions-environment-remembering\/","title":{"rendered":"Arrow Functions \u2013 Environment Remembering"},"content":{"rendered":"<p>Arrow functions are mainly intended to\u00a0resolve several common pain points of traditional Function Expression. it will provide\u00a0Lexical <em>this<\/em> binding and Short hand notations with &#8216;=&gt;&#8217; arrow symbol.<br \/>\n<strong>Definition (ECMA6 Standard):<\/strong> Arrow functions bind <em>this<\/em> lexically, bind <em>return<\/em> in the Block body case so it returns from the immediately enclosing arrow function.<br \/>\n<strong>Syntax:<\/strong> ArrowParameters [no <em>LineTerminator<\/em> here] =&gt; ConciseBody<br \/>\n[table class=&#8221;table table-striped&#8221;]<br \/>\nArrow Function Syntax,Arrow Function Examples<br \/>\n(parameters) -&gt; expression,&#8221;(x,y) =&gt; x * y&#8221;<br \/>\n(parameters) -&gt; statement,() =&gt; console.log(&#8220;Hello JS&#8221;);<br \/>\n(parameters) -&gt; { statements },(s) =&gt; { n = s.length(); return n;}<br \/>\n[\/table]<br \/>\n<strong>Simply:<\/strong> which provide a shorter notation of anonymous functions + which binds only lexical <em>this<\/em>, no dynamic <em>this<br \/>\n<\/em><strong>Pros:\u00a0<\/strong><\/p>\n<ul>\n<li>Much easier for the compiler to optimize, because it doesn&#8217;t need to worry about variables escaping from their lexical context, and so doesn&#8217;t need to go for chaining lookup.<\/li>\n<li>Scope safety, Compactness, and Readability<\/li>\n<li>An arrow function expression has a shorter syntax compared to function expressions<br \/>\nLexically binds the this value<\/li>\n<li>Arrow functions are always anonymous<\/li>\n<li>Arrow functions cannot be used as constructors.<\/li>\n<li>Arrow functions never have an arguments objects<\/li>\n<li>&#8220;=&gt;&#8221; has only lexical <em>this<\/em>, no dynamic <em>this<\/em><\/li>\n<\/ul>\n<p><strong>Cons:\u00a0<\/strong>Not fit when a dynamic context is required like defining methods and get the target from\u00a0this\u00a0when handling events.<br \/>\n<strong>Lexical (Static) VS Dynamic Scope:<\/strong><br \/>\nLexical: Creates a new copy of variable names and values, organized in a hierarchy called \u201cthe environment\u201d.<br \/>\nDynamic: All variable names and their values live in one global table.<br \/>\n<strong>Common mistakes of using Arrow Functions:<\/strong><\/p>\n<ul>\n<li>new (() =&gt; {}) throws TypeError: (intermediate value) is not a constructor<\/li>\n<li>a = (() =&gt; {});a.arguments;<br \/>\nThrows TypeError: &#8216;caller&#8217; and &#8216;arguments&#8217; are restricted function properties and cannot be accessed in this context.<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-113\" src=\"https:\/\/www.bhoomabrsr.com\/blog\/wp-content\/uploads\/2016\/09\/JSA.jpg\" alt=\"JSA\" width=\"2100\" height=\"1771\" \/><br \/>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Arrow functions are mainly intended to\u00a0resolve several common pain points of traditional Function Expression. it will provide\u00a0Lexical this binding and Short hand notations with &#8216;=&gt;&#8217; arrow symbol. Definition (ECMA6 Standard): Arrow functions bind this lexically,&hellip; <a href=\"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/2016\/09\/03\/arrow-functions-environment-remembering\/\">More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,14,15,19],"tags":[],"class_list":["post-112","post","type-post","status-publish","format-standard","hentry","category-ecma-2015","category-ecma6","category-javascript","category-simplecards"],"_links":{"self":[{"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/112","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=112"}],"version-history":[{"count":0,"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/112\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=112"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}