<?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: Anoop P K</title>
    <description>The latest articles on DEV Community by Anoop P K (@anoopaneesh).</description>
    <link>https://dev.to/anoopaneesh</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%2F761755%2F16fe0ec5-0626-45d7-b846-553673315145.jpeg</url>
      <title>DEV Community: Anoop P K</title>
      <link>https://dev.to/anoopaneesh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/anoopaneesh"/>
    <language>en</language>
    <item>
      <title>Factory functions with private variables in JavaScript</title>
      <dc:creator>Anoop P K</dc:creator>
      <pubDate>Sun, 23 Jun 2024 07:16:41 +0000</pubDate>
      <link>https://dev.to/anoopaneesh/factory-functions-with-private-variables-in-javascript-4efk</link>
      <guid>https://dev.to/anoopaneesh/factory-functions-with-private-variables-in-javascript-4efk</guid>
      <description>&lt;p&gt;We all know classes in JavaScript , it is a template for creating objects.Classes are created using the "&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes"&gt;class&lt;/a&gt;" keyword in JS. But before the introduction of class we had an alternate way of achieving this object oriented approach - Factory Functions&lt;/p&gt;

&lt;p&gt;Factory functions are simple javascript functions which returns an object&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3pm3ovooa1gqka4vo9r5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3pm3ovooa1gqka4vo9r5.png" alt="Factory function example" width="800" height="544"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note that we don't require a "new" keyword to create the object.&lt;/p&gt;

&lt;p&gt;The "this" keyword used inside the function will refer to the execution context (environment in which the function is executed) of that function.&lt;/p&gt;

&lt;p&gt;Now we have private variables in classes , Let us see how we can achieve this using factory functions.&lt;/p&gt;

&lt;p&gt;We can make use of Closures in JavaScript. What is a Closure?&lt;br&gt;
A &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures"&gt;closure&lt;/a&gt; is the collection of variables and functions that is being referenced. &lt;/p&gt;

&lt;p&gt;The variables used inside a function cannot be accessed outside unless it returns the variable.&lt;/p&gt;

&lt;p&gt;We can make use of these to simulate private variables using factory functions&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnjtutiu9d9erogwqyvnv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnjtutiu9d9erogwqyvnv.png" alt="Private variables using factory function" width="800" height="521"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here the variables id and marks cannot be used outside the function since it is block scoped. But it can be accessed by the inner function (status) , because it forms a closure with those variables.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;References&lt;/strong&gt;&lt;br&gt;
Classes : &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes&lt;/a&gt;&lt;br&gt;
Closures : &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures&lt;/a&gt;&lt;br&gt;
Execution Context : &lt;a href="https://www.freecodecamp.org/news/how-javascript-works-behind-the-scene-javascript-execution-context/"&gt;https://www.freecodecamp.org/news/how-javascript-works-behind-the-scene-javascript-execution-context/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>functional</category>
      <category>factory</category>
    </item>
  </channel>
</rss>
