<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sayan Singha(@dvlpr_ind) on Medium]]></title>
        <description><![CDATA[Stories by Sayan Singha(@dvlpr_ind) on Medium]]></description>
        <link>https://medium.com/@sayansingha?source=rss-927d458efaa8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*gJDnbuldLQjwBfR2ePF3bA.jpeg</url>
            <title>Stories by Sayan Singha(@dvlpr_ind) on Medium</title>
            <link>https://medium.com/@sayansingha?source=rss-927d458efaa8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 09:40:43 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sayansingha/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[CSS: Pseudo-Classes and Pseudo-Elements]]></title>
            <link>https://sayansingha.medium.com/css-pseudo-classes-and-pseudo-elements-b6f3df66a031?source=rss-927d458efaa8------2</link>
            <guid isPermaLink="false">https://medium.com/p/b6f3df66a031</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[css]]></category>
            <category><![CDATA[css3]]></category>
            <category><![CDATA[web]]></category>
            <category><![CDATA[web-design]]></category>
            <dc:creator><![CDATA[Sayan Singha(@dvlpr_ind)]]></dc:creator>
            <pubDate>Thu, 11 May 2023 08:38:04 GMT</pubDate>
            <atom:updated>2023-05-11T08:38:04.910Z</atom:updated>
            <content:encoded><![CDATA[<p>All about how to use pseudo-classes and pseudo-elements in CSS.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*inHK4N1QAmf4DGlAdIv63w.jpeg" /></figure><h3>Introduction:</h3><p>Pseudo-classes and pseudo-elements are an awesome way to implement CSS that we normally cannot do in normal CSS classes or elements. In this blog I hope to explain what pseudo-elements and pseudo-classes are and how to use them in your own projects.</p><h3>Pseudo-Elements:</h3><p>Pseudo-elements are used to style a specific part of an element.<br>For example it can be used to:<br>1. Style the first letter, or line, of an element<br>2. Insert content before, or after, the content of an element</p><h4>Syntax:</h4><p>The syntax of pseudo-elements:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d2bbd7b3a783e5e329e405dc4c5738cb/href">https://medium.com/media/d2bbd7b3a783e5e329e405dc4c5738cb/href</a></iframe><h4>Examples:</h4><p>Some examples of pseudo-elements:<br>In this example the <em>first-letter</em> of <em>p</em> tag is of a different colour.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fsayansingha%2Fembed%2Fpreview%2FRweMzeX%3Fdefault-tabs%3Dhtml%252Cresult%26height%3D600%26host%3Dhttps%253A%252F%252Fcodepen.io%26slug-hash%3DRweMzeX&amp;display_name=CodePen&amp;url=https%3A%2F%2Fcodepen.io%2Fsayansingha%2Fpen%2FRweMzeX&amp;image=https%3A%2F%2Fshots.codepen.io%2Fusername%2Fpen%2FRweMzeX-512.jpg%3Fversion%3D1683706407&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/5d6023abef16b6c6b6a81cc636d9ff18/href">https://medium.com/media/5d6023abef16b6c6b6a81cc636d9ff18/href</a></iframe><p>In this example there is a combination of two pseudo-elements of <em>first-letter</em> and <em>first-line</em>:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fsayansingha%2Fembed%2Fpreview%2FWNazqWp%3Fdefault-tabs%3Dhtml%252Cresult%26height%3D600%26host%3Dhttps%253A%252F%252Fcodepen.io%26slug-hash%3DWNazqWp&amp;display_name=CodePen&amp;url=https%3A%2F%2Fcodepen.io%2Fsayansingha%2Fpen%2FWNazqWp&amp;image=https%3A%2F%2Fshots.codepen.io%2Fusername%2Fpen%2FWNazqWp-512.jpg%3Fversion%3D1683706825&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/ef218686324e5b1dd1a8a9b1ae53c87e/href">https://medium.com/media/ef218686324e5b1dd1a8a9b1ae53c87e/href</a></iframe><h3>Pseudo-Classes:</h3><p>Similar to <em>pseudo-elements</em>, <em>pseudo-classes</em> are css keywords that are added to the selector to style a specific special state of the selected element.<br>For example:<br>1. Style an element when it is hovered<br>2. Different styles for visited and unvisited links</p><h3>Syntax:</h3><p>The syntax of pseudo-classes:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b876000170075594301562fd28f01b22/href">https://medium.com/media/b876000170075594301562fd28f01b22/href</a></iframe><h3>Examples:</h3><p>Some examples for pseudo-classes:<br>In this example the <em>:hover </em>pseudo class is used:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fsayansingha%2Fembed%2Fpreview%2FMWPGGwr%3Fdefault-tabs%3Dcss%252Cresult%26height%3D600%26host%3Dhttps%253A%252F%252Fcodepen.io%26slug-hash%3DMWPGGwr&amp;display_name=CodePen&amp;url=https%3A%2F%2Fcodepen.io%2Fsayansingha%2Fpen%2FMWPGGwr&amp;image=https%3A%2F%2Fshots.codepen.io%2Fusername%2Fpen%2FMWPGGwr-512.jpg%3Fversion%3D1683791463&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/a19db4327182ea2282ba86ea54596351/href">https://medium.com/media/a19db4327182ea2282ba86ea54596351/href</a></iframe><p>In this example we have the <em>:visited</em>, <em>:link</em>, <em>:hover</em>, <em>:active</em> pseudo classes:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fsayansingha%2Fembed%2Fpreview%2FMWPGXEN%3Fdefault-tabs%3Dcss%252Cresult%26height%3D600%26host%3Dhttps%253A%252F%252Fcodepen.io%26slug-hash%3DMWPGXEN&amp;display_name=CodePen&amp;url=https%3A%2F%2Fcodepen.io%2Fsayansingha%2Fpen%2FMWPGXEN&amp;image=https%3A%2F%2Fshots.codepen.io%2Fusername%2Fpen%2FMWPGXEN-512.jpg%3Fversion%3D1683793829&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/7901391050e771f0e3de53b88d55a263/href">https://medium.com/media/7901391050e771f0e3de53b88d55a263/href</a></iframe><h3>Conclusion:</h3><p>Hoping that this blog helped you in someway to better understand the pseudo-elements and pseudo-classes used in css. Have fun using them in your own code.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b6f3df66a031" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[CSS — Types of Selectors]]></title>
            <link>https://sayansingha.medium.com/css-types-of-selectors-9cf3af3a7c2e?source=rss-927d458efaa8------2</link>
            <guid isPermaLink="false">https://medium.com/p/9cf3af3a7c2e</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[css]]></category>
            <category><![CDATA[html]]></category>
            <category><![CDATA[web-design]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Sayan Singha(@dvlpr_ind)]]></dc:creator>
            <pubDate>Fri, 28 Oct 2022 13:11:05 GMT</pubDate>
            <atom:updated>2022-10-29T10:37:11.240Z</atom:updated>
            <content:encoded><![CDATA[<h3>CSS — Types of Selectors</h3><h4>The most essential CSS ingredient, all in one blog.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/720/1*aJO-vsouAMv6W5L72L3spQ.jpeg" /></figure><h4>Introduction</h4><p>CSS — Selectors selects the HTML element(s) that we want to style. It is one of the most essential element of CSS, that enables us to do a lot!</p><h4>Types of CSS — Selectors:</h4><h4>Basic Selectors:</h4><p><strong>Universal Selectors(*):- </strong><br>The * is used to denote universal selectors, and matches elements of all type.<br>Example:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fsayansingha%2Fembed%2Fpreview%2FMWXYMQK%3Fdefault-tabs%3Dhtml%252Cresult%26height%3D600%26host%3Dhttps%253A%252F%252Fcodepen.io%26slug-hash%3DMWXYMQK&amp;display_name=CodePen&amp;url=https%3A%2F%2Fcodepen.io%2Fsayansingha%2Fpen%2FMWXYMQK&amp;image=https%3A%2F%2Fshots.codepen.io%2Fsayansingha%2Fpen%2FMWXYMQK-512.jpg%3Fversion%3D1666994501&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/d45db1d1dab3fffb2457cad288f7f9ef/href">https://medium.com/media/d45db1d1dab3fffb2457cad288f7f9ef/href</a></iframe><p><strong>Type selectors:<br></strong>Selects all elements that have the given element/node name.<br>Example:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fsayansingha%2Fembed%2Fpreview%2FxxzboMP%3Fdefault-tabs%3Dhtml%252Cresult%26height%3D600%26host%3Dhttps%253A%252F%252Fcodepen.io%26slug-hash%3DxxzboMP&amp;display_name=CodePen&amp;url=https%3A%2F%2Fcodepen.io%2Fsayansingha%2Fpen%2FxxzboMP&amp;image=https%3A%2F%2Fshots.codepen.io%2Fsayansingha%2Fpen%2FxxzboMP-512.jpg%3Fversion%3D1666962297&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/27da7ba5782cb18d82e0d8bd500a448c/href">https://medium.com/media/27da7ba5782cb18d82e0d8bd500a448c/href</a></iframe><p><strong>Class Selectors:-<br></strong>Class selectors matches elements based on the contents of the class attribute of the elements.<br>Example:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fsayansingha%2Fembed%2Fpreview%2FzYaxgNW%3Fdefault-tabs%3Dhtml%252Cresult%26height%3D600%26host%3Dhttps%253A%252F%252Fcodepen.io%26slug-hash%3DzYaxgNW&amp;display_name=CodePen&amp;url=https%3A%2F%2Fcodepen.io%2Fsayansingha%2Fpen%2FzYaxgNW&amp;image=https%3A%2F%2Fshots.codepen.io%2Fsayansingha%2Fpen%2FzYaxgNW-512.jpg%3Fversion%3D1666962352&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/a9d2f75200452f5db1f61bd90154a2f8/href">https://medium.com/media/a9d2f75200452f5db1f61bd90154a2f8/href</a></iframe><p><strong>ID Selectors:-<br></strong>The ID selectors implements the changes on the elements with the matching id attribute.<br>Example:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fsayansingha%2Fembed%2Fpreview%2FYzvPmrb%3Fdefault-tabs%3Dhtml%252Cresult%26height%3D600%26host%3Dhttps%253A%252F%252Fcodepen.io%26slug-hash%3DYzvPmrb&amp;display_name=CodePen&amp;url=https%3A%2F%2Fcodepen.io%2Fsayansingha%2Fpen%2FYzvPmrb&amp;image=https%3A%2F%2Fshots.codepen.io%2Fsayansingha%2Fpen%2FYzvPmrb-512.jpg%3Fversion%3D1666962404&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/0662b30cc6adbfa16bfd5dcf0a7ab37d/href">https://medium.com/media/0662b30cc6adbfa16bfd5dcf0a7ab37d/href</a></iframe><p><strong>Attribute Selectors:-<br></strong>Here the changes in the CSS will apply to the specified attribute of an element.<br>Example:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fsayansingha%2Fembed%2Fpreview%2FPoaqOPe%3Fdefault-tabs%3Dhtml%252Cresult%26height%3D600%26host%3Dhttps%253A%252F%252Fcodepen.io%26slug-hash%3DPoaqOPe&amp;display_name=CodePen&amp;url=https%3A%2F%2Fcodepen.io%2Fsayansingha%2Fpen%2FPoaqOPe&amp;image=https%3A%2F%2Fshots.codepen.io%2Fsayansingha%2Fpen%2FPoaqOPe-512.jpg%3Fversion%3D1666961160&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/092868bdf5e8b867afb7c0fa0bb47210/href">https://medium.com/media/092868bdf5e8b867afb7c0fa0bb47210/href</a></iframe><h4>Group Selectors:</h4><p><strong>List selectors(,):</strong><br>The list selector is ‘<strong>,</strong>’. Two different elements separated by a comma(<strong>,</strong>) can have the same styling, and we won’t have to write two different styling for those elements.<br>Example:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fsayansingha%2Fembed%2Fpreview%2FgOKpXGj%3Fdefault-tabs%3Dhtml%252Cresult%26height%3D600%26host%3Dhttps%253A%252F%252Fcodepen.io%26slug-hash%3DgOKpXGj&amp;display_name=CodePen&amp;url=https%3A%2F%2Fcodepen.io%2Fsayansingha%2Fpen%2FgOKpXGj&amp;image=https%3A%2F%2Fshots.codepen.io%2Fsayansingha%2Fpen%2FgOKpXGj-512.jpg%3Fversion%3D1666962060&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/755f5c7ef6a370f2f48ca5d223273efc/href">https://medium.com/media/755f5c7ef6a370f2f48ca5d223273efc/href</a></iframe><h4>Conclusion:</h4><p>These selectors are the most used selectors in web development and these will be enough to build any high level web pages. I hope the concepts discussed above and the examples help a lot.</p><p>Hoping to see you in the next blog when we discuss <em>Pseudo Classes</em> and <em>Pseudo Elements</em> in CSS.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9cf3af3a7c2e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The S.O.L.I.D Principles — Clean Code]]></title>
            <link>https://sayansingha.medium.com/the-s-o-l-i-d-principles-clean-code-9fcca658dab5?source=rss-927d458efaa8------2</link>
            <guid isPermaLink="false">https://medium.com/p/9fcca658dab5</guid>
            <category><![CDATA[function]]></category>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[solid]]></category>
            <category><![CDATA[clean-code]]></category>
            <dc:creator><![CDATA[Sayan Singha(@dvlpr_ind)]]></dc:creator>
            <pubDate>Fri, 30 Apr 2021 13:32:58 GMT</pubDate>
            <atom:updated>2021-04-30T13:32:58.748Z</atom:updated>
            <content:encoded><![CDATA[<h3>The S.O.L.I.D Principles — Clean Code</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*e_TzFJ_ZIYeaQm2d029Stw.jpeg" /></figure><blockquote>Clean code always looks like it was written by someone who cares. — Robert C. Martin</blockquote><p>The principle of clean coding is extremely essential and the SOLID principles are outlined to help in that aspect. Every developer must have a basic understanding of these principles and that’s what this blog will aim to achieve, to give a brief overview of these principles.</p><p>SOLID principles are 5 concepts that will help us make our programs better.</p><ul><li>Single Responsibility Principle</li><li>Open / Closed Principles</li><li>Liskov Substitution Principle</li><li>Interface Segregation Principle</li><li>Dependency Inversion Principle</li></ul><h3><strong>Single Responsibility Principle:</strong></h3><p>The single responsibility principle says that each of our classes has to be only used for one purpose.</p><p>We need this so that we don’t have to change code as often when something changes. It’s also hard to understand what the class is doing if it’s doing many things.</p><p>Unrelated concepts in the same class also make comprehending the purpose of the code harder.</p><h3><strong>Open / Closed Principle:</strong></h3><p>The open/closed principle states that a piece of software is open for extension but closed for modification.</p><p>This means that we should be able to add more functionality without changing existing code.</p><h3><strong>Liskov Substitution Principle:</strong></h3><p>This principle states that if we have a parent class and a child class, then we can interchange the parent and child class without getting incorrect results.</p><p>This means that the child class must implement everything that’s in the parent class. The parent class serves the class has the base members that child classes extend from.</p><p>For example, if we want to implement classes for a bunch of shapes, we can have a parent Shape class, which are extended by all classes by implementing everything in the Shape class.</p><h3><strong>Interface Segregation Principle:</strong></h3><p>The interface segregation principle states that “clients shouldn’t be forced to depend on interfaces that they don’t use.”</p><p>This means that we shouldn’t impose the implementation of something if it’s not needed.</p><h3><strong>Dependency Inversion Principle:</strong></h3><p>This principle states that high-level modules shouldn’t depend on low-level modules and they both should depend on abstractions, and abstractions shouldn’t depend upon details. Details should depend upon abstractions.</p><p>This means that we shouldn’t have to know any implementation details of our dependencies. If we do, then we violated this principle.</p><p>We need this principle because if we do have to reference the code for the implementation details of a dependency to use it, then when the dependency changes, there’s going to be lots of breaking changes to our own code.</p><p>As software gets more complex, if we don’t follow this principle, then our code will break a lot.</p><h3><strong>Conclusion</strong></h3><p>In conclusion, I hope you got a basic idea of the SOLID principles that help us write much cleaner codes and increase the maintainability and helps us better refactor the code.</p><p>I hope to write a more in depth blog on each of these principles with JavaScript code examples which might further help.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9fcca658dab5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Factory Functions vs Classes: JavaScript]]></title>
            <link>https://sayansingha.medium.com/factory-functions-vs-classes-javascript-4c550dad1966?source=rss-927d458efaa8------2</link>
            <guid isPermaLink="false">https://medium.com/p/4c550dad1966</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[function]]></category>
            <category><![CDATA[class]]></category>
            <category><![CDATA[functional-programming]]></category>
            <dc:creator><![CDATA[Sayan Singha(@dvlpr_ind)]]></dc:creator>
            <pubDate>Fri, 30 Apr 2021 13:11:22 GMT</pubDate>
            <atom:updated>2021-04-30T13:11:22.603Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1023/1*r8QzSRf6pt4SxecJ_k1t-w.jpeg" /></figure><p>Recently while trying to understand clean code in JS, I ran upon the differences in between factory functions and classes and how they affect the code readability, refactoring and the general advantages and disadvantages of using each.</p><h3><strong>Factory Functions:</strong></h3><p>Let’s start with factory functions, quite simply functions in JS can return objects, when they are neither constructor functions or classes, it’s called factory function.</p><p>An example of a factory function would be:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2b3cca7435971c68e2d57801063e55c5/href">https://medium.com/media/2b3cca7435971c68e2d57801063e55c5/href</a></iframe><p>The above code basically creates an object named dog1 with a function that logs something in the console.</p><h3><strong>Benefits of Factory Functions:</strong></h3><p>Factory functions are very flexible and they don’t lead us into the dark road of inheritance, deep inheritance hierarchies specifically. The other benefits include:</p><p><strong>1.Less problems with refactoring issues:</strong></p><p>For starters we will never have to convert factory functions to classes or constructor functions so automatically refactoring is much less of an issue.</p><p><strong>2.No ‘<em>new</em>’ keyword:</strong></p><p>No ambiguity of using the ‘new’ keyword.</p><p><strong>3.’<em>this</em>’ behaves in a standard manner:</strong></p><p>‘<em>this</em>’ behaves as it normally would, so you can use it to access the parent object. Also, inside any function call of an object of factory function, ‘this’ will work as it normally should.</p><h3><strong>Drawbacks of Factory Functions:</strong></h3><p>It <strong>may</strong> perform slower than a constructor function in micro-optimization benchmarks. The slow path is still very fast — millions of ops/sec on an old computer. This is more likely to be a concern in library or framework code than in application code.</p><h3><strong>Classes:</strong></h3><p>Classes are basically prototype of something and they usually have member variables and member functions, in order to create an object and execute it’s behaviour.</p><p>An example of class would be:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b624c331c8e9673aae9d543a045d8c45/href">https://medium.com/media/b624c331c8e9673aae9d543a045d8c45/href</a></iframe><p>The above example works similar to the above factory function but obviously this has a different syntax and how the object is created, especially the use of the “<em>new”</em> keyword.</p><p>But the code works the same i.e., we create an object dog2 that executes the function bark when called and prints something in the console.</p><h3><strong>Benefits of Classes:</strong></h3><ol><li><em>’this’ refers to the new object.</em></li><li>There may be a micro-optimization performance benefit, but we should not worry about that unless we have profiled your code and proven that it’s an issue for us.</li><li>Convenient and self contained syntax.</li><li>More familiar to people with a class-based language background.</li></ol><h3><strong>Drawbacks of using Classes:</strong></h3><ol><li>Temptation for users to create problematic class hierarchies using inheritance.</li><li>Required ‘<em>new</em>’ and the ambiguity it brings with it.</li><li>Classes break the Open / Closed principle of clean coding.</li></ol><h3><strong>Conclusion</strong></h3><p>In my opinion and experience, factory function and functional programming as a whole is far more convenient, easy to maintain, easy to refactor and debug, with much less downside of performance issue.</p><p>For larger codes, these problems are even more evident and it can be seen when inheritance especially can cause a much bigger problem for programmers.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4c550dad1966" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[“You Don’t Know JS:” My Understanding of Scopes and Closures]]></title>
            <link>https://medium.com/better-programming/you-dont-know-js-my-understanding-of-scopes-closures-e0d2bfe4c328?source=rss-927d458efaa8------2</link>
            <guid isPermaLink="false">https://medium.com/p/e0d2bfe4c328</guid>
            <category><![CDATA[javascript-tips]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[coding]]></category>
            <dc:creator><![CDATA[Sayan Singha(@dvlpr_ind)]]></dc:creator>
            <pubDate>Tue, 12 Nov 2019 17:15:22 GMT</pubDate>
            <atom:updated>2019-11-13T18:27:33.096Z</atom:updated>
            <content:encoded><![CDATA[<p>In the quest to understand JavaScript in as much depth as possible, reading the <a href="https://github.com/getify/You-Dont-Know-JS">YDKJS</a> book series by Kyle Simpson is a must. Here’s my understanding of the second book, “<a href="https://github.com/getify/You-Dont-Know-JS/blob/2nd-ed/scope-closures/README.md">Scope &amp; Closures</a>,” in this amazing book series.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/277/1*KB0m1gkDbhqJsV_wa2ISXQ.jpeg" /><figcaption><a href="https://github.com/getify/You-Dont-Know-JS/blob/2nd-ed/scope-closures/README.md">“Scope &amp; Closure” by Kyle Simpson</a></figcaption></figure><p>The inclusion of variables in our program is one of the most basic requirements to perform tasks. But it also begets the questions:</p><p>1. Where do these variables live, or where are they stored?<br>2. How does our program find them when it needs them?</p><p>The set of well-defined rules that speak on the above questions and more is called <em>scope.</em></p><h3>Compiler Theory</h3><p>Despite the fact JavaScript falls under the general category of a <em>dynamic</em> or <em>interpreted</em> language, it’s, in fact, a <em>compiled</em> language. For simplicity’s sake, I won’t delve into the various steps of compilation.</p><p>Any snippet of JavaScript has to be compiled before (usually <em>right</em> before!) it’s executed. So the JS compiler will take the program var a = 2; and compile it first and then be ready to execute it — usually right away.</p><p>If we take the example of var a = 2;, the JavaScript engine will break down var a = 2;into separate chunk characters, parse those chunks into what’s called an <em>abstract syntax tree,</em> and then turn that tree into a set of machine instructions to actually create a variable called a if not previously declared in the current scope(in the compilation phase) and later assign it the value of 2 (in the execution phase).</p><h3>Compiler Lookups</h3><p>Here the author introduces the concept of left-hand side (LHS) and right-hand side (RHS) lookups. But the question is: to the side of <em>what?</em> To the side o<strong>f </strong>an assignment operation.</p><p>The LHS lookup is done when a variable appears on the left-hand side of the assignment operation, as in the var a = 2; example, where the LHS lookup is trying to find the variable container itself so it can assign.</p><p>The RHS lookup is done when a variable is on the right-hand side of an assignment operation, as in console.log(a);.<strong> </strong>The reference to a<strong> </strong>is an<strong> </strong>RHS reference because nothing is being assigned to a here. Instead, we’re looking up to retrieve the value of a so the value can be passed to console.log(…). By contrast, in a = 2;, the reference to a is an LHS reference because we don’t actually care what the current value is — we simply want to find the variable as a target for the =2 assignment operation.</p><p>If the variable can’t be found in the scope chain for an LHS lookup, a global variable will automatically be created (when in nonstrict mode), or a ReferenceError will be thrown (when in strict mode).</p><p>If the variable can’t be found in the scope chain for an RHS lookup, a ReferenceError will be thrown.</p><p>So whenever you see a ReferenceError in the console, you can be sure it relates to a scope failure.</p><h3>Lexical Scopes</h3><p><em>Lexical scope</em> is a scope that is defined by the JavaScript author at the time of writing the code. (This is a nice way of thinking about scope!) It all comes down to where the author chooses to place his/her variables and functions (and blocks). This placement is what tells the JavaScript engine where it should perform its LHS and RHS lookups — in other words, the chain of scopes it needs to move up through to find a variable.</p><h4>JavaScript uses the lexical scope</h4><p>It gets easier and more interesting to understand and code when we approach the concept of nested scopes by looking at them from the point of view of RHS and LHS.</p><p>Consider:</p><pre>function foo(a) {<br>	console.log( a + b );<br>}<br><br>var b = 2;<br><br>foo( 2 ); // 4</pre><p>The RHS reference for b can’t be resolved inside the function foo, but it can be resolved in the scope surrounding it (in this case, it’s the global scope).</p><h4>The simple rules for traversing nested scope</h4><p>We start at the currently executing scope and look for the variable there. Then if not found, we keep going up one level — and so on. If the outermost global scope is reached, the search stops, whether it finds the variable or not.</p><p>The book gives a really great example of nested scopes as “bubbles inside of each other.”</p><h3>Cheating Lexical</h3><p>Before I discuss the two cheats or ways to modify the lexical scope, I must give a warning — for mainly two reasons:</p><ol><li>It’s a bad practice and is frowned upon by the wider JavaScript community</li><li>It leads to poorer performance</li></ol><p>And, therefore, I’ll mention them in not much detail. Nevertheless, it’s interesting. OK, here we go:</p><h4>eval</h4><p>Consider this:</p><pre>function foo(str, a) {<br>	eval( str ); // cheating!<br>	console.log( a, b );<br>}<br><br>var b = 2;<br><br>foo( &quot;var b = 3;&quot;, 1 ); // 1 3</pre><p>Quite simply, you can see that the eval(..) takes a string and treats the contents of the string as if it had actually been authored code at that point in the program.</p><p>In other words, you can programmatically generate code inside your authored code and run the generated code as if it had been there at author time.</p><h4>with</h4><p>with is typically explained as a shorthand for making multiple property references against an object without repeating the object reference itself each time.</p><pre>var obj = {<br>	a: 1,<br>	b: 2,<br>	c: 3<br>};<br><br>// more &quot;tedious&quot; to repeat &quot;obj&quot;<br>obj.a = 2;<br>obj.b = 3;<br>obj.c = 4;<br><br>// &quot;easier&quot; short-hand<br>with (obj) {<br>	a = 3;<br>	b = 4;<br>	c = 5;<br>}</pre><h3>Hiding Stuff in Functions</h3><p>Here we introduce the <em>principle of least exposure</em>. As the name suggests, we should expose only what is minimally necessary and, essentially, hide everything else.</p><h4>Collision avoidance</h4><p>Another benefit of hiding variables and functions inside a scope is to avoid unintended collisions between two different identifiers with the same name but different intended usages. Collisions often result in the unexpected overwriting of values.</p><p>For example:</p><pre>function foo() {<br>	function bar(a) {<br>		i = 3; // changing the `i` in the enclosing scope&#39;s for-loop<br>		console.log( a + i );<br>	}</pre><pre>	for (var i=0; i&lt;10; i++) {<br>		bar( i * 2 ); // oops, infinite loop ahead!<br>	}<br>}</pre><pre>foo();</pre><p>The i = 3 assignment inside of bar(..) overwrites, unexpectedly, the i that was declared in foo(..) at the for-loop.</p><p>In this case, it’ll result in an infinite loop, because i is set to a fixed value of 3, and that’ll forever remain &lt; 10.</p><h3>Function Declaration vs. Function Expression</h3><p>This is fairly a very important concept I was completely oblivious to.</p><p>Simply put, if function is the first thing in the statement, then it’s a function declaration. Otherwise, it’s a function expression.</p><p>Function declaration:</p><pre>function foo() { <br>	console.log(&quot;This is a function declaration&quot;);<br><br>}</pre><p>Consider this:</p><pre>var a = 2;</pre><pre>(function foo(){ // &lt;-- insert this</pre><pre>	var a = 3;<br>	console.log( a ); // 3</pre><pre>})(); // &lt;-- and this</pre><pre>console.log( a ); // 2</pre><p>The (function foo(){ .. }) as an expression means the identifier foo is found only in the scope where the .. indicates, not in the outer scope. Hence, the values inside the scope that encloses (function foo(){ .. }) are not polluted by what’s in the global scope.</p><p>The following are the different ways of writing a function expressions:</p><pre>var foo = function() { <br>	console.log(&quot;This is an anonymous function expression&quot;);<br><br>}</pre><pre>var x = function foo() { <br>	console.log(&quot;This is a <em>named</em> function expression&quot;);<br><br>}</pre><pre>(function() { <br>	console.log(&quot;This is a self-invoking function expression&quot;);<br><br>})()</pre><pre>(function foo() { <br>	console.log(&quot;This is a named self-invoking function expression&quot;);<br>})()</pre><pre>setTimeout( function(){<br>	console.log(&quot;This is an anonymous function expression as a callback parameter&quot;);<br>}, 1000 );</pre><pre>setTimeout( function(){<br>	console.log(&quot;This is a named function expression as a callback parameter&quot;);<br>}, 1000 );</pre><h3>Immediately Invoked Function Expression (IIFE)</h3><p>IIFE can be written in two different ways:</p><pre>(function() {<br>  console.log(&#39;First&#39;)<br>})();</pre><pre>(function() {<br>  console.log(&#39;Second&#39;)<br>}());</pre><h3>Blocks As Scopes</h3><p>Consider a for loop:</p><pre>for (var i=0; i&lt;10; i++) {<br>	console.log( i );<br>}<br>console.log(i) //10</pre><p>Now, we can clearly see that it makes a kind of sense that we declare the i inside the for loop, but it’s not relevant when using var because they’ll always belong to the enclosing scope. Then, why pollute the entire scope of a function with the i variable that’s only going to be used in the for loop? The solution to that is let and const. Let’s take a look at them:</p><p>Let and const create a block scope of their own, as does the catch in a try…catch statement.</p><p>The following examples will clear it further:</p><h4>try…catch</h4><pre>try {<br>	undefined(); // illegal operation to force an exception!<br>}<br>catch (err) {<br>	console.log( err ); // works!<br>}<br><br>console.log( err ); // ReferenceError: `err` not found</pre><p>As you can see, err exists only in the catch clause and throws an error when you try to reference it elsewhere.</p><h4>let</h4><pre>var foo = 1;<br><br>if (foo) {<br>	let bar = foo * 2;<br>	console.log( bar );// 2<br>}<br><br>console.log( bar ); // ReferenceError</pre><p>The same code when run with var:</p><pre>var foo = 1;<br><br>if (foo) {<br>	var bar = foo * 2;<br>	console.log( bar );// 2<br>}<br><br>console.log( bar ); // 2</pre><h4>const</h4><pre>var foo = 1;<br><br>if (foo) {<br>	var a = 2;<br>	const b = 3; // block-scoped to the containing `if`<br><br>	a = 3; // just fine!<br>	b = 4; // error!<br>}<br><br>console.log( a ); // 3<br>console.log( b ); // ReferenceError!</pre><p>Const also creates a block-scoped variable, but the value is fixed (constant). Any attempt to change that value at a later time results in an error.</p><h3>Hoisting</h3><p>This is, perhaps, the most important conclusion of everything we’ve learned above — and maybe the most interesting too.</p><p>Let&#39;s look at two scenarios:</p><pre>Scenario 1:<br>a = 2;<br><br>var a;<br><br>console.log( a ); // 2</pre><pre>Scenario 2:<br>console.log( a ); // undefined<br><br>var a = 2;</pre><p>Now the reason why the outputs might seem confusing is because of hoisting.</p><p>The JS engine works in two steps: The first is the <em>compilation</em> phase, and the next is the <em>execution</em> phase. The work of these two steps is different.</p><p>In the compilation phase, all variables and function declarations are processed.</p><p>After the compilation phase, in the execution phase, all the logic and assignments take place.</p><p>When you see var a = 2;, you probably think of that as one statement. But JavaScript actually thinks of it as two statements: var a; and a = 2;. The first statement, the declaration, is processed during the compilation phase. The second statement, the assignment, is left <em>in place</em> for the execution phase.</p><p><strong>Note:</strong> In the above example, the <em>in place</em> part is very important. It’s also important to note that hoisting is per scope.</p><p>Our first snippet will then be:</p><pre>var a;</pre><pre>a = 2;<br><br>console.log( a ); // 2</pre><p>And our second snippet:</p><pre>var a;</pre><pre>console.log( a ); //undefined<br><br>a = 2;</pre><h4>Functions first</h4><p>A subtle yet important detail: Functions are hoisted first — and then variables.</p><p>Consider:</p><pre>foo(); // 1</pre><pre>var foo;</pre><pre>function foo() {<br>    console.log( 1 );<br>}</pre><pre>foo = function() {<br>    console.log( 2 );<br>};</pre><p>1 is printed instead of 2. This snippet is interpreted by the engine as:</p><pre>function foo() {<br>    console.log( 1 );<br>}</pre><pre>foo(); // 1</pre><pre>foo = function() {<br>    console.log( 2 );<br>};</pre><p>While multiple/duplicate var declarations are effectively ignored, subsequent function declarations do override previous ones.</p><p>Consider the next two snippets:</p><p>Snippet 1:</p><pre>foo(); // 3 since the second function decalaration overrides the first one.<br><br>function foo() {<br>    console.log( 1 );<br>}<br><br>var foo = function() {<br>    console.log( 2 );<br>};<br><br>function foo() {<br>    console.log( 3 );<br>}</pre><p>Snippet 2:</p><pre>foo(); // &quot;b&quot;<br><br>var a = true;<br>if (a) {<br>   function foo() { console.log( &quot;a&quot; ); }<br>}<br>else {<br>   function foo() { console.log( &quot;b&quot; ); }<br>}</pre><p>Function declarations that appear inside of normal blocks typically hoist to the enclosing scope, rather than being conditional, as the above code implies.</p><h3>Closure</h3><p>Closure is a fairly simple concept to understand if one understands lexical scope well enough. Essentially, we use closure quite often but without really understanding it.</p><p>We can look at closure as a way of calling a function or using a function out of its lexical scope or the ability of the function to access its lexical scope even when that function is called outside its lexical scope.</p><p>Consider this simple code:</p><pre>function foo() {<br>	var a = 2;<br><br>	function bar() { // this function has a closure over the scope of foo<br>		console.log( a ); <br>	}<br><br>	bar();<br>}<br><br>foo(); // 2</pre><p>To make the above code even clearer, consider this:</p><pre>function foo() {<br>	var a = 2;<br><br>	function bar() {<br>		console.log( a );<br>	}<br><br>	return bar; // we return the entire function of bar<br>}<br><br>var baz = foo(); // now baz has the same function definition as bar but it can acess bar&#39;s lexical scope<br><br>baz(); // guess what the output is, yes it&#39;s the same as above: 2</pre><p>So to define it:</p><blockquote>Closure is when a function is able to remember and access its lexical scope — even when that function is executing outside its lexical scope.</blockquote><h3>Modules</h3><p>I was not familiar with the idea of <em>modules</em> as explained in this book, but they’re very interesting and helpful.</p><p>Modules are code patterns that leverage the power of closure.</p><p>Take this for an example:</p><pre>function Module() {<br>	var a= &quot;This is a module!&quot;;<br>	<br>	function func() {<br>		console.log( a );<br>	}<br><br>	return {<br>		func: func<br>	};<br>}<br><br>var foo = Module();<br>foo.func(); // This is a module!</pre><p>This is the pattern in JavaScript we call module.</p><p>To state it more simply, there are two requirements for the module pattern to be exercised:</p><ol><li>There must be an outer-enclosing function, and it must be invoked at least once (in the above example it is Module).</li><li>The outer-enclosing function must return back at least one inner function so this inner function has closure over the private scope and can access and/or modify that private state. (In the above example, it is func<em>.</em>)</li></ol><h3>Done</h3><p>It’s now time to delve into the third book on <a href="https://github.com/getify/You-Dont-Know-JS/blob/2nd-ed/objects-classes/README.md">this and object prototypes</a></p><p>Happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e0d2bfe4c328" width="1" height="1" alt=""><hr><p><a href="https://medium.com/better-programming/you-dont-know-js-my-understanding-of-scopes-closures-e0d2bfe4c328">“You Don’t Know JS:” My Understanding of Scopes and Closures</a> was originally published in <a href="https://betterprogramming.pub/">Better Programming</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>