<?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 Swapan Shaw on Medium]]></title>
        <description><![CDATA[Stories by Swapan Shaw on Medium]]></description>
        <link>https://medium.com/@swapan.1.shaw?source=rss-ea99efe5c1d9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*BVgmWO-pYF1Z6_Pu.</url>
            <title>Stories by Swapan Shaw on Medium</title>
            <link>https://medium.com/@swapan.1.shaw?source=rss-ea99efe5c1d9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 05:47:23 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@swapan.1.shaw/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[Lexical scoping and function scope in Javascript]]></title>
            <link>https://medium.com/@swapan.1.shaw/lexical-scoping-and-function-scope-in-javascript-13216aceb07a?source=rss-ea99efe5c1d9------2</link>
            <guid isPermaLink="false">https://medium.com/p/13216aceb07a</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Swapan Shaw]]></dc:creator>
            <pubDate>Tue, 09 May 2017 12:01:03 GMT</pubDate>
            <atom:updated>2017-05-09T12:14:09.878Z</atom:updated>
            <content:encoded><![CDATA[<p>Lexical scoping and function scope in Javascript</p><p>I believe as a programmer it is very important to understand how the variable scope work in the language you write code. With this thought I am writing this blog about variable and function scope works in javascript.</p><p><strong>Lexical Scoping</strong> defines how variable names are resolved in nested functions. Other names of <em>Lexical Scoping</em> are <em>Static Scoping</em> or <em>Closure</em>. It means that the scope of an <em>inner function</em> contains the scope of a <em>parent function</em>. Let’s have a look at the following example</p><pre>void fun()<br>{<br>    int a = 5;<br><br>    void fun2()<br>    {<br>        console.log(a);// print 5 in the console<br>    }<br>}</pre><p>If you look at the above example then inner function fun2 have access to the outer level function ‘fun’ variable ‘a’ and inside the inner function variable name ‘a’ is resolved.</p><p><strong>Dynamic Scoping : </strong>In dynamic scoping outer scope is dynamic and depends on the chain call of the functions. Dynamic scoping is like passing references of all variables to the called function. lets look at the example below.</p><pre>void fun()<br>{<br>    console.log(x);<br>}<br><br>void func1()<br>{<br>    varx = 5;<br><br>    fun();<br>}<br><br>void func2()<br>{<br>    var x = 10;<br><br>    fun();<br>}</pre><p>Above sample code I wrote to make you understand, how dynamic scoping works. Here fun can either access x in func1or func2, or any x in any function that call fun with x declared in it.</p><blockquote><em>Some languages, like </em><strong><em>Perl</em></strong><em> and </em><strong><em>Common Lisp</em></strong><em>, allow the programmer to choose static or dynamic scoping when defining or redefining a variable. Examples of languages that use dynamic scoping include Logo, Emacs </em><strong><em>Lisp</em></strong><em>, and the shell languages </em><strong><em>bash</em></strong><em>, dash, and </em><strong><em>PowerShell</em></strong><em>.</em></blockquote><p>There is a little more to it, and that little more is what makes <em>Lexical Scoping</em> so powerful: <em>inner functions</em> contain the scope of <em>parent functions</em> even if the parent function has returned.<strong> </strong>this is called as <strong>closure.</strong> Lets try to underand with below sample code.</p><blockquote>var adder = function (total) {</blockquote><blockquote><em>// the following function is returned </em><br> <em>// and assigned to adder</em><br> var inner_function = function (number) {<br> total += number;<br> alert(total);<br> }<br> return inner_function;<br>}(0); <em>// &lt;- we call the annonymous function </em><br> <em>// and assign the returned function to adder</em></blockquote><blockquote>adder(2); <em>// -&gt; 2</em><br>adder(3); <em>// -&gt; 5</em></blockquote><p>Anyway, the inner_function that is returned is assigned to the variable adder. And when you call adder, that is the inner_function, it has access to total due to <em>Lexical Scoping</em>, even though the function that had the total. total itself was declared in the scope of a function that has returned a long time ago.</p><p>And that’s it. You know everything there is to know about <em>Lexical Scoping</em>: <strong>inner functions contain the scope of parent functions even if the parent function has returned.</strong></p><p>Some languages, like <strong>Perl</strong> and <strong>Common Lisp</strong>, allow the programmer to choose static or dynamic scoping when defining or redefining a variable. Examples of languages that use dynamic scoping include Logo, Emacs <strong>Lisp</strong>, and the shell languages <strong>bash</strong>, dash, and <strong>PowerShell</strong>.</p><p>In JavaScript we have both <strong><em>Function Scope</em></strong> and <strong><em>Lexical Scoping</em>.</strong></p><p><strong>Function Scope</strong> means that any variable which is defined within a function is visible within that <em>entire function</em>. This is quite different form <em>Block Scope</em>, in which a variable scope is limited by the <em>block</em> a variable is declared in. A block is usually what you find between {curly braces} or loop variables. <em>Block Scope</em> is used in most of the programming languages that are currently popular. Think of a for(;;){} loop. In ES6 javascript do support the block scope when a variable is declare with <strong>‘let’</strong>. This feature is available in javascript with ES6.</p><blockquote>function sayHi(){</blockquote><blockquote>for(var i =0 ; i &lt;5; i++){</blockquote><blockquote>console.log(Hi + ‘Joe’);</blockquote><blockquote>}</blockquote><blockquote>}</blockquote><p>Here the variable i is access from anywhere inside the sayHi function. It is not only access with the for loop block. We can solve this issue using ES6 new keyword ‘let’.</p><blockquote>function sayHi(){</blockquote><blockquote>for(let i =0 ; i &lt;5; i++){</blockquote><blockquote>console.log(Hi + ‘Joe’);</blockquote><blockquote>}</blockquote><blockquote>}</blockquote><p>Now i access is limited to inside the for loop only.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=13216aceb07a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[IIFE ( Immediately Invoked Functions Expression) in Javascript]]></title>
            <link>https://medium.com/@swapan.1.shaw/javascript-iife-immediately-invoked-functions-expression-3234b8b8847?source=rss-ea99efe5c1d9------2</link>
            <guid isPermaLink="false">https://medium.com/p/3234b8b8847</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Swapan Shaw]]></dc:creator>
            <pubDate>Tue, 04 Apr 2017 07:16:41 GMT</pubDate>
            <atom:updated>2017-05-10T10:10:36.819Z</atom:updated>
            <content:encoded><![CDATA[<p>As a javascript programmer we all know in javascript there is concept of anonymous function, those who do not know the concept of anonymous function please go through the concept before reading IIFE. IIFE in javascript is an anonymous function which is self invoked. The below code snippet is the syntax to create an IIFE.</p><p>IIFE can be written in the several ways. Below code snippet present the common convention followed by developers to write an IIFE expression. This is also known as the Douglas Crockford’s style.</p><p>Code snippet:</p><blockquote>(function(){</blockquote><blockquote>})()</blockquote><p>IIFE expression can be used to avoid <strong>variable hoisting</strong> from within block, protect against <strong>polluting the global environment</strong> and allow <strong>public access to method</strong> while retaining the privacy of variables.</p><p>The primary reason to use an IIFE is to obtain data privacy. Because JavaScript’s var scopes variables to their containing function, any variables declared within the IIFE cannot be accessed by the outside world.</p><blockquote><em>(</em><strong><em>function</em></strong><em> () {</em></blockquote><blockquote><strong><em>var</em></strong><em> foo</em><strong><em>=</em></strong><em> “hello”;</em></blockquote><blockquote><em>console.log(foo); // Outputs: “Hello”</em></blockquote><blockquote><em>})();</em></blockquote><blockquote><em>console.log(foo);</em></blockquote><blockquote><em>// ReferenceError: foo is not defined</em></blockquote><p>We can achieve the same thing as IIFE by declaring a function and invoked the function as below code snippet.</p><blockquote>function imediatlyInvokedFunction(){<br> var greet= ‘hello world’;</blockquote><blockquote>console.log(greet);</blockquote><blockquote>}</blockquote><blockquote>imediatlyInvokedFunction();</blockquote><p>If we can achieve the same behavior as IIFE with the above code snippet then why we need IIFE?</p><p>Answer is the above code as some disadvantages. First one is we can call the imediatlyInvokedFunction as many times from our code, but IIFE can be executed only once. Second one is imediatlyInvokedFunction function declaration takes up names in the global namespace this could leads to a name collision.</p><p>You can point out that how can we pass arguments to the IIFE which is declare in the parent scope chain of the IIFE. We can achieve the same with below code snippet where we can inject the variable or object or function as dependency to the IIFE.</p><blockquote>var greet = ‘hello World’;</blockquote><blockquote>(function($, _, msg){</blockquote><blockquote>console.log(msg);</blockquote><blockquote>})(JQuery, undescore, greet,);</blockquote><p>As you can see in the above code base that the JQuery and underscore object is injected apart from your custom variable/objects/functions greet etc.</p><p>I would encourage you all to run/play around the below code snippet and try to understand the behavior of IIFE and what is will print in the console.</p><p>var foo=10;</p><p>(function(){</p><p>console.log(foo);</p><p>var foo = 20;</p><p>})();</p><p>Thank you. Thats all I about IIFE.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3234b8b8847" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>