<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Victor Miranda</title>
    <description>The latest articles on Forem by Victor Miranda (@vcctm).</description>
    <link>https://forem.com/vcctm</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F804348%2F519ddb3d-b2fd-44a4-83e6-698ed8eb01fc.jpeg</url>
      <title>Forem: Victor Miranda</title>
      <link>https://forem.com/vcctm</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/vcctm"/>
    <language>en</language>
    <item>
      <title>What is a closure? And why this matters ? - Javascript</title>
      <dc:creator>Victor Miranda</dc:creator>
      <pubDate>Tue, 11 Oct 2022 22:01:43 +0000</pubDate>
      <link>https://forem.com/vcctm/what-is-a-closure-and-why-this-matters-javascript-386p</link>
      <guid>https://forem.com/vcctm/what-is-a-closure-and-why-this-matters-javascript-386p</guid>
      <description>&lt;h1&gt;
  
  
  Closures
&lt;/h1&gt;

&lt;p&gt;Maybe you already have done an interview with the following question: Describe a closure in javascript? &lt;br&gt;
But what is closure? and why this concept matters for modern development with &lt;strong&gt;React.js&lt;/strong&gt; for example?&lt;/p&gt;

&lt;p&gt;For definition in MDN an closure are: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function's scope from an inner function. In JavaScript, closures are created every time a function is created, at function creation time.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So if you are a beginner js developer maybe you don't have familiarity with those concepts like inner function, outer function, scope, and that's understandable, nowadays developers are caring less about how js works in the background and just jump into developing new things with react and stuff, I don't wanna judge, I took the same path as well, but if you are like me, and now feeling that you should need to understand better how those things works, this gonna be a good article for you! 🚀&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6pj21hxi6efty7t1ptfq.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6pj21hxi6efty7t1ptfq.gif" alt="scope being declared"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Scope: how works and what is this?
&lt;/h4&gt;

&lt;p&gt;Are you seeing this gif? Why does this work? How is it possible to &lt;code&gt;getPerson&lt;/code&gt; function to access &lt;code&gt;pet&lt;/code&gt; variable even if, pet variable is not inside of it? &lt;br&gt;
Fault: &lt;strong&gt;scope&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When we invoked the function &lt;code&gt;getPerson&lt;/code&gt;, what's happening? In memory, we set a space for each context, in this case as default for any code a &lt;strong&gt;global context&lt;/strong&gt;(&lt;em&gt;can be window in browsers and global in Node&lt;/em&gt;) and we set also the &lt;code&gt;getPerson&lt;/code&gt; function context(&lt;em&gt;local function context&lt;/em&gt;). Each context has its own scope, and &lt;code&gt;getPerson&lt;/code&gt; function have a scope similar like that.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8u1xeph0trj8xn1uiq1d.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8u1xeph0trj8xn1uiq1d.gif" alt="variable contexts"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Look, we have two context and inside each one we also obtain: &lt;strong&gt;variables&lt;/strong&gt;, &lt;strong&gt;this&lt;/strong&gt;, and for only for locally context of an function: &lt;strong&gt;args&lt;/strong&gt;, I don't want to talk about the last two ones but basically they are cores inside local contexts. But I ask you how &lt;code&gt;getPerson&lt;/code&gt; doesn't get undefined using &lt;code&gt;pet&lt;/code&gt; variable I will explain:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5ure0we2l9ydtzg2rvtt.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5ure0we2l9ydtzg2rvtt.gif" alt="declaring flux"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So because of hoisting, we have all the variables declarations occurring when the program starts, but their values will be associated just in runtimes, so before &lt;code&gt;getPerson&lt;/code&gt; get executed we have all the global context being completed, and when &lt;code&gt;getPerson&lt;/code&gt; function runs, we search for variable &lt;code&gt;pet&lt;/code&gt; inside the local context, if don't find, go search for the outer context, inside the actual scope until reach the variable declaration and value, otherwise throwing an error for variables that are not found. This behavior is called scope chain, and this matters for closures.&lt;/p&gt;

&lt;p&gt;So you understand about scope chain, how this is mixed with closure definition? Getting back to the MDN definition, let's take a look at the last part of it: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In other words, a closure gives you access to an outer function's scope from an inner function. In JavaScript, closures are created every time a function is created, at function creation time.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I should say, in my words, a closure for definition: it's just the case when a function uses an outer function scope, but this is not might right in my mind so I came up with this other definition: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A closure is a stack frame that is allocated when a function starts its execution, and not freed after the function returns (as if a 'stack frame' were allocated on the heap rather than the stack!). In JavaScript, you can think of a function reference variable as having both a pointer to a function as well as a hidden pointer to a closure.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Forgetting about the heap, and stack, there are other javascript concepts, you might think, a closure uses an outer variable and persist this data after the function execution, in other languages like C, after a function got executed, we free the memory an all variables memory related with that, but for javascript we can persist this variable.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frcnh8uzh2fikuyglnjrv.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frcnh8uzh2fikuyglnjrv.gif" alt="closure working"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Look this gif, we see &lt;code&gt;matFunction&lt;/code&gt; being declared and all the context and scopes chains with it, &lt;code&gt;add&lt;/code&gt; and &lt;code&gt;decrease&lt;/code&gt; functions inside &lt;code&gt;matFunction&lt;/code&gt; have access by &lt;strong&gt;scope&lt;/strong&gt; chain to the &lt;code&gt;matFunction&lt;/code&gt; &lt;strong&gt;context&lt;/strong&gt;, being able to edit and persist the value of &lt;strong&gt;x&lt;/strong&gt; &lt;strong&gt;variable&lt;/strong&gt;, all this is only able because we declare the &lt;strong&gt;mat&lt;/strong&gt; &lt;strong&gt;variable&lt;/strong&gt; in the &lt;strong&gt;global&lt;/strong&gt; &lt;strong&gt;context&lt;/strong&gt; as &lt;code&gt;matFunction&lt;/code&gt; execution, this create a space in heap for x variable can changed and &lt;strong&gt;persisted&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;And that's it, &lt;strong&gt;&lt;code&gt;add&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;decrease&lt;/code&gt;&lt;/strong&gt; functions are closures for x variable, and we can persist their value on memory.&lt;/p&gt;

&lt;p&gt;NEXT: Why closure are important for react?&lt;/p&gt;

&lt;p&gt;If you like, please share with your friends, this take work to made haha!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>react</category>
    </item>
  </channel>
</rss>
