<?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>DEV Community: MohamedElshazly</title>
    <description>The latest articles on DEV Community by MohamedElshazly (@mohamedelshazly).</description>
    <link>https://dev.to/mohamedelshazly</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%2F517885%2Fedf43b15-e4f6-47cc-a3fe-4ff15b25c94e.jpeg</url>
      <title>DEV Community: MohamedElshazly</title>
      <link>https://dev.to/mohamedelshazly</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mohamedelshazly"/>
    <language>en</language>
    <item>
      <title>Cool Closure things! (part 1)</title>
      <dc:creator>MohamedElshazly</dc:creator>
      <pubDate>Tue, 16 May 2023 21:29:09 +0000</pubDate>
      <link>https://dev.to/mohamedelshazly/cool-closure-things-part-1-372k</link>
      <guid>https://dev.to/mohamedelshazly/cool-closure-things-part-1-372k</guid>
      <description>&lt;p&gt;You can implement functions like once using closures! &lt;/p&gt;

&lt;p&gt;it uses the concept of scope, or "backpack".&lt;/p&gt;

&lt;p&gt;Basically when you have a function that also returns a function, that returned function gets the variables and data around it (accessible to it in the same scope) in its "backpack", or more officially, in its [[scope]] hidden/private property! &lt;/p&gt;

&lt;p&gt;That allows us to do pretty cool stuff, like for example, create an implementation of the 'once' function!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const creator = (cb) =&amp;gt; {
  let counter = 0;
  function once() {
    if(counter &amp;gt;= 1) {
      console.log("Function only runs once!");
    }
    else {
      counter++;
      console.log(cb());
    }
  }
  return once;
}
const callback  = (a, b) =&amp;gt; {
  return a+b;
}
const newOnce = creator(() =&amp;gt; callback(1, 6));

newOnce();
newOnce();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;let's explain what this code does: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Here we have a closure called creator, it takes a callback, that does whatever we want, here however, it simply adds two numbers together.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const creator = (cb) =&amp;gt; {
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;then we define a counter variable to track the number of times we ran the code&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let counter = 0;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;We check the value of counter if it exceeds 1, then we don't execute the callback code, otherwise, we go ahead with the execution&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function once() {
if(counter &amp;gt;= 1) {
  console.log("Function only runs once!");
}
else {
  counter++;
  console.log(cb());
}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;we finally return the function&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;return once;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;One of the coolest things here is that the memory is permanent! the function returned from the closure remembers its previous state, which is really powerful! &lt;/p&gt;

&lt;p&gt;I learned this feature recently when I was studying js and closures, and thought it would be cool to try and explain it, hope it would be useful to someone. &lt;/p&gt;

&lt;p&gt;Cheers!&lt;/p&gt;

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