<?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 Naveen Karippai on Medium]]></title>
        <description><![CDATA[Stories by Naveen Karippai on Medium]]></description>
        <link>https://medium.com/@naveenkarippai?source=rss-bce54079a820------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*0eIJUUiOX-MwFgtpe24ULA.png</url>
            <title>Stories by Naveen Karippai on Medium</title>
            <link>https://medium.com/@naveenkarippai?source=rss-bce54079a820------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 11:31:57 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@naveenkarippai/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[Debunking “fake” classes in JavaScript]]></title>
            <link>https://medium.com/@naveenkarippai/debunking-fake-classes-in-javascript-78f67a6b5c96?source=rss-bce54079a820------2</link>
            <guid isPermaLink="false">https://medium.com/p/78f67a6b5c96</guid>
            <category><![CDATA[object-oriented]]></category>
            <category><![CDATA[metaprogramming]]></category>
            <category><![CDATA[es6]]></category>
            <category><![CDATA[ecmascript-6]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Naveen Karippai]]></dc:creator>
            <pubDate>Mon, 12 Dec 2016 20:35:50 GMT</pubDate>
            <atom:updated>2023-01-14T09:57:30.053Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>“And you think you’re so clever and classless and free” ~John Lennon</blockquote><p><strong><em>TL;DR</em></strong>: <em>ECMAScript6 introduced </em><em>class keyword to provide (native) class-based programming in JavaScript. It is essentially a syntactic sugar on top of the prototype-based programming model in JavaScript. It gives us cleaner syntax compared to complex Function Constructors.</em></p><p>JavaScript is a high-level, dynamic, and loosely-typed programming language. It is highly object-oriented to the core with its prototype-based model, inspired by<a href="https://en.wikipedia.org/wiki/Self_(programming_language)"> <strong><em>Self</em></strong></a> programming language.</p><p>For the developers coming from class-based programming world (Java), here is the news: prototype-based programming is an OOP model that does NOT make use of classes, but rather it imitates the ideology of inheritance in the class-oriented world by decorating (expanding) on prototype objects, and leveraging on the technique of prototype delegation. In simple words, JavaScript objects do the cloning and NOT inheritance. <strong><em>ECMAScript6 has introduced </em></strong><strong>class<em> keyword as syntactic sugar on top of the prototype-based model</em></strong> to give developers a choice, and write cleaner syntax on prototype-based code.</p><p><strong><em>Bottom line:</em></strong></p><ol><li>ECMAScript6 has introduced class keyword as syntactic sugar on top of the (existing) prototype-based programming model, and there is no change happening to this model</li><li>class-based keywords provide developers a choice of coding style to choose and help to write cleaner syntax</li><li>class-based keywords can be useful but NOT flexible, or powerful as prototype-based meta programming using Function Constructors</li></ol><p><strong><em>An aside:</em></strong></p><p>Unlike many traditional class-based programming languages (Java), ECMAScript6 has many important class-based programming features (still) missing. The <a href="https://github.com/tc39">TC39</a> committee has plans already to augment class-based programming in the post ECMAScript6 time frame.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/1*Y0YBFpkc8QWaCo3gPoRarQ.jpeg" /><figcaption>src: <a href="https://imgflip.com/">imgflip</a></figcaption></figure><h4><strong><em>Quick glimpse on Terminology</em></strong></h4><p><strong>class</strong>: It acts as a blue print for defining object’s properties and methods</p><p><strong>object</strong>: It is an instance of the class and can be a combination of data structures, variables and functions</p><p><strong>method</strong>: It is a function defined on a class</p><p><strong>constructor</strong>: It is a special method (automatically) invoked when an object is instantiated</p><h4><strong><em>ES6 class declaration and its pre-ES6 equivalent</em></strong></h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/12faac53539c04ef848319268fbe3f05/href">https://medium.com/media/12faac53539c04ef848319268fbe3f05/href</a></iframe><p>The code snippet above shows the process of class declaration on <em>ECMAScript6</em>. We should note that new keyword is used for instantiating an object from a class. That said, instanceof operator can be used to check if an object was instantiated from a particular class, it yields a boolean value to notify the same.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c67e3e777abaf790c7a34b4857ca1dd6/href">https://medium.com/media/c67e3e777abaf790c7a34b4857ca1dd6/href</a></iframe><p>The code snippet above shows declaration of Function Constructor in <em>pre-ES6 </em>(equivalent to class declaration in ES6). We can see that instance variables are declared on this in Function object, and the methods are defined on Function.prototype object. There will be a reference created automatically between Function and Function.prototype objects through __proto__ property on Function object during instantiation using new keyword. The methods on Function.prototype will be called through the process of <em>prototype delegation</em> on instance object.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/515/1*xlLRQifCk2QlsrHCuo2PIw.png" /><figcaption>Tool: UMLet (GNU Linux client)</figcaption></figure><p>Essentially, we get the same prototype chain on class and Function Constructor declarations.</p><p><strong><em>Result:</em></strong></p><ol><li>properties defined on constructor in ES6 can be mapped to properties defined on Function object in ES5</li><li>methods defined on classes in ES6 can be mapped to properties (methods) declared on Function.prototype object in ES5</li><li>class in ES6 can be thought of as an instruction to JavaScript compiler to automatically populate the prototype object</li></ol><p><strong><em>An aside:</em></strong></p><ol><li>class declarations are NOT hoisted, so it can be instantiated with new keyword only after its declaration. But, Function constructors are hoisted.</li><li>instanceof operator can be used to find if an object was instantiated from a particular class or Function Constructor. It tests whether an object has in its prototype chain the prototypeproperty of a constructor.</li><li>It’s a good style convention to set <em>uppercase </em>name for class or Function Constructor as this would notify the developer to call new keyword on object instantiation statement.</li></ol><h4>Class expressions</h4><p>Similar to function expressions, we can define a class as an expression instead of declaration. This can be either named or unnamed, and like in named function expressions, named class expression will have its name local to class’ body. That said, class expressions does NOT support hoisting.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/26a117c414d2cf4a2044b8d5f0438b69/href">https://medium.com/media/26a117c414d2cf4a2044b8d5f0438b69/href</a></iframe><p>class expressions could be useful to pass it as a function argument, or assign it as an object property.</p><h4>“extends” keyword</h4><p>In traditional programming languages (Java), extends keyword is used to define a child class (subclass, derived class) that <strong><em>inherits</em></strong> properties (methods and variables) from a parent class (super class, base class). In ECMAScript6, we have extends keyword to create a child class from a parent class. I should highlight the fact that JavaScript makes use of <strong><em>prototype delegation </em></strong>(instead<strong><em> </em></strong>of<strong><em> inheritance</em></strong>)<strong><em> </em></strong>to invoke properties (methods) on parent class from an instance of child class. This behavior of prototype delegation is often misunderstood as inheritance by some developers.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e9c66e966a74434d5446adecfdbc4970/href">https://medium.com/media/e9c66e966a74434d5446adecfdbc4970/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/817/1*t7_P95hVTA-9q6gtMG49Ag.png" /><figcaption>Tool: UMLet (GNU Linux client)</figcaption></figure><p>In the code snippet above, when we invoke run() method on the object instance superman, it looks up the property (method) on Superman.prototype object. As run() (property) method is NOT defined there, it continues the search on its immediate prototype object along the prototype chain (Flash.prototype). It then invokes run() method found on Flash.prototype object. The extends keyword in ES6 provides us a cleaner syntax for subclassing compared to the Function Constructor meta-programming style.</p><p><strong><em>ELI5: </em></strong>The<strong><em> prototype delegation</em></strong> can be described as the technique by which on invocation of an object property, the object looks up for the property in its own body. If the property is defined on its own body, then it returns the value. And, if NOT found, then it would continue to look for the property on its immediate prototype object, which is referenced through __proto__ property. This process of searching through prototype chain would continue until the property is (first) found and if NOT found in the prototype chain objects, it would yield undefined.</p><p>From meta-programming perspective in JavaScript, we can consider extends keyword as an equivalent to <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create">Object.create()</a> method.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ae40793301880a8feaf7a9c71be029f8/href">https://medium.com/media/ae40793301880a8feaf7a9c71be029f8/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/407/1*vBb_8kWOdLpAia433anvdQ.png" /><figcaption>Tool: UMLet (GNU Linux client)</figcaption></figure><p>The Object.create() method can be used to create a new object with the specified prototype object and (optional) properties. In the code snippet above, when we invoke property named fly on spiderman object, it looks for the property on its own body. As it is NOT defined there, it continues to search for the property on its (immediate) prototype object ironman, and then returns the value from prototype object through the process of prototype delegation.</p><h4>“super” keyword</h4><p>The super keyword can be used to invoke methods on an object’s parent. Besides, when super keyword is defined in a constructor function, it must be used before the this keyword.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bf5369ca059238af997a2e13101bffa7/href">https://medium.com/media/bf5369ca059238af997a2e13101bffa7/href</a></iframe><p>In the code snippet above, we are invoking run() method on parent class (Flash) using super keyword on child class (Quicksilver). The super keyword could be useful when we want to invoke overriden methods on child class.</p><p>From meta-programming perspective in JavaScript, we can consider super keyword as an equivalent to Object.getPrototypeOf() method.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f91936614bc9767e09634ef8994bd5f5/href">https://medium.com/media/f91936614bc9767e09634ef8994bd5f5/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/331/1*Yspl_-2E2ZROggU38L7Zng.png" /><figcaption>Tool: UMLet (GNU Linux client)</figcaption></figure><p>The Object.getPrototypeOf() method returns the prototype (value of the __proto__ property) of the specified object. It can be used to invoke overriden properties from immediate prototype (parent) object. In the code snippet above, we are creating an object arrow with its prototype object set to object atom. As both objects contain the property named greet, the child object arrow, when invoked, overrides the property on object atom(parent object). The property defined on parent object atom can be accessed through calling Object.getPrototypeOf() method with the property name.</p><h4>Constructor</h4><p>The constructor method is a special method for creating and initializing an object created with a class. Unlike some traditional class-based programming languages (Java), we can define only one constructor method in a class. Constructors are not mandatory for class declaration in JavaScript. And, if omitted, there will be a default constructor (automatically) substituted by JavaScript compiler. On the child class, we must invoke super() method inside the constructor before accessing the this. This happens due to the fact that parent class is responsible for initializing the this of object instance created.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ce8788cec0d213164c7f657cf7a5dd83/href">https://medium.com/media/ce8788cec0d213164c7f657cf7a5dd83/href</a></iframe><h4>“static” keyword</h4><p>The <strong><em>static</em></strong> keyword can be used to define a static method for a class. They are often used to create utility functions, and are callable directly from class and NOT from instances of the class. The static properties (methods) are added on Function object, instead of Function prototype object. In simple words, static properties can be used when we want methods that are common to all objects (instances).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0e5516f1036dfc33f9cc5727fcbeb38c/href">https://medium.com/media/0e5516f1036dfc33f9cc5727fcbeb38c/href</a></iframe><p>In the code snippet above, we have declared a static method in class Wolverine. The static method can be invoked without instantiation of class.</p><h4>Extending native objects</h4><p>The extends keyword in ECMAScript6 provides us an easy way to subclass native JavaScript objects like Array, Object. This was possible in pre-ES6 time frame, but should say, it was NOT simple.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ca0f21cb28a9dcab4c60a27af3af6629/href">https://medium.com/media/ca0f21cb28a9dcab4c60a27af3af6629/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/264/1*zkfdvkP8WOBdlbx77YUUOg.png" /><figcaption>Tool: UMLet (GNU Linux client)</figcaption></figure><p>In the code snippet above, we are creating a subclass ‘Cisco’ from parent class (native object) Array using extends keyword. Thus, we can call Array.prototype methods on the subclass’ (Cisco) object instance (cisco) through the process of prototype delegation. In comparison to JavaScript libraries like <a href="http://prototypejs.org/"><em>prototype.js</em></a><em>,</em> which augments native prototype objects directly, the ES6 approach of extending native objects by subclassing (using extends keyword) could be more efficient and recommended.</p><h4>Conclusion</h4><p>Good knowledge of JavaScript prototype-based programming model and familiarity with ES6 class-based keywords can help developers to write clean code.</p><p>Happy coding!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=78f67a6b5c96" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Learning to debug with Git]]></title>
            <link>https://medium.com/@naveenkarippai/learning-to-debug-with-git-20bf13318164?source=rss-bce54079a820------2</link>
            <guid isPermaLink="false">https://medium.com/p/20bf13318164</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[git]]></category>
            <category><![CDATA[debugging]]></category>
            <category><![CDATA[github]]></category>
            <category><![CDATA[version-control]]></category>
            <dc:creator><![CDATA[Naveen Karippai]]></dc:creator>
            <pubDate>Sat, 19 Nov 2016 15:57:30 GMT</pubDate>
            <atom:updated>2023-01-14T09:58:14.701Z</atom:updated>
            <content:encoded><![CDATA[<p><strong><em>TL;DR</em></strong>: Git, the stupid content tracker, provides us with a couple of tools to debug the issues in our project. It could be easier to find the author of a specific line of code, or search for the changeset that introduced a bug in the project by leveraging on <em>git tools</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*go7UVRBQXo1GEnCgTA9uYg.jpeg" /><figcaption>src: <a href="https://memegenerator.net/">memegenerator</a></figcaption></figure><p><strong><em>Bottom line:</em></strong></p><ol><li>git grep command can be used to search for a string, or a regular expression in the working directory (project)</li><li>git blame command can be used to find the author of a specific line of code, or the commit (SHA-1) that introduced it</li><li>git bisect tool can be used to find out the commit that introduced the bug in the project</li></ol><p><strong><em>Tip of the day: </em></strong>run $ man git [command] on the terminal to see the manual of a specific git command</p><p><strong><em>A quick introduction on Git:</em></strong></p><p>Git, the stupid content tracker, is a fast and scalable distributed version control system that is designed to manage projects with efficiency. It is free and open-sourced.</p><p>For this post, we will be using Git on the command line (terminal). As an alternative, you can either checkout built-in git GUI tools for committing [git gui] and browsing [gitk], or third-party git GUI clients [git cola].</p><h3><strong><em>git grep</em></strong></h3><p><strong><em>When? </em></strong>you want to find a string or a regular expression in any of the files in the working directory (project).</p><p><strong><em>Why? </em></strong>It can be efficient and faster with git grep to search in the project when we are running a <em>machine with low specs</em>. Also, it avoids searching through <em>gitignore</em>-d files, by default.</p><p><strong><em>How? </em></strong>we can execute $ git grep [string|RegExp] on the terminal to return the list of tracked files in the working directory, that contains our search keyword.</p><p><em>Example: </em>$ git grep &#39;downloadImages&#39;</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/769/1*Y3-iR9vyaP7q81OaXHVGUg.png" /><figcaption>Terminal: Z shell on GNU Linux</figcaption></figure><p><strong><em>Bonus: </em></strong>we could use option -n, --line-number to show the line number of matching lines of code. Also, using option -i, --ignore-case would ignore case differences between search keyword and file.</p><p><em>Example:</em> $ git grep -in &#39;John Lennon&#39;</p><p><strong><em>An aside: </em></strong>we can search for a keyword in a <em>specific </em>file by setting its file path while running git grep command. [EDIT] That said, “grep” stands for Globally search with Regular Expression and Print.</p><p><em>Example:</em> $ git grep &#39;John Lennon&#39; Music/TheBeatles.txt</p><h3>git blame</h3><p><strong><em>When?</em></strong> you want to find the commit (SHA-1) that introduced the specific line of code that causes a bug in the project. Also, you want to find the name of the author for a specific line of code, to ask for more information, or to blame ;)</p><p><strong><em>Why?</em></strong> It shows you the name of the author for a specific line of code, to ask for further information, or to blame. By getting commit (SHA-1) for a specific line of code, you can show the changeset that introduced it, for getting additional information.</p><p><em>Example:</em> run $ git show #SHA-1 to show the commit</p><p><strong><em>How? </em></strong>we can run $ git blame filePath/fileName to show the annotation of a specific file, with commit (SHA-1) of each line of code in it, and what person authored each commit.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/881/1*EEnC6VNgC0bG2y8fQw5zQA.png" /><figcaption>Terminal: Z shell on GNU Linux</figcaption></figure><p><strong><em>Bonus:</em></strong> we could use the simple shell script in the code snippet below, to show the names of all contributors on a specific file in the project, along with the information on number of lines contributed by each of them. This would help us to identify the first person to go for more information on a specific file in the project.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/381f415683feb0e19f8ccfddc5ff6e69/href">https://medium.com/media/381f415683feb0e19f8ccfddc5ff6e69/href</a></iframe><p><em>Example:</em> $ git contr filepath/filename</p><p><strong><em>An aside:</em></strong> use option -L to limit the output of git blame to specific lines of code. And, use option -C to see where the sections of code originally came from. Basically, it tells us the original author and commit (SHA-1) regardless of the refactoring done afterward.</p><p><em>Example:</em> $ git blame -LC 10,15 filepath/filename</p><h3>git bisect</h3><p><strong><em>When?</em></strong> you want to find the git commit that introduced the bug on a project, which contains (let us say) 100+ commits. Also, you <em>don’t</em> know what file in the project contains the bug.</p><p><strong><em>Why?</em></strong> git bisect is essentially a binary search over the git commit tree to identify the first “bad” commit. This is (obviously) efficient and faster than trying to find “bad” commit <em>manually</em> by checking-out git commits in random.</p><p><strong><em>How?</em></strong></p><ol><li>we can run the command $ git bisect start to initiate with binary search mode on git to find a bug.</li><li>we need to tag a “good” commit (bug-free commit) from git commit history. Therefore, we should log the git commit history to find a “good” commit. But, let us set the option --oneline on git log command to see only the name (subject) of git commits. For <em>example</em>: $ git log --oneline. Then, find a “good” commit (SHA-1), and run $ git bisect good #SHA-1 to inform git about the “good” commit.</li><li>we need to find the “bad” commit (bug-present commit) for git to do a binary search between “good” and “bad” commits, and thereby find the bug. Since the latest commit has the bug, usually it can be assigned as the “bad” commit. For <em>example</em>: $ git bisect bad #SHA-1.</li><li>repeat (2) and (3) while git walks us through git commit history and tag “good”/“bad” commits.</li><li>when we have successfully found the <strong>first</strong> “bad” commit (source of bug), we can exit the git binary search mode by executing $ git bisect reset.</li></ol><p><strong><em>Bonus:</em></strong> you can automate git bisect by writing unit tests that identify the bug, and running them automatically. The code snippet below should do the trick.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/feff7f35e6b71ac84db65412850d3bf0/href">https://medium.com/media/feff7f35e6b71ac84db65412850d3bf0/href</a></iframe><p><strong><em>An aside:</em></strong> we should only search for a single bug in the process of running git bisect. If we need to find multiple bugs, then run git bisect them separately for each of the bugs. Also, to get further information, or inspect a “bad” commit, we can run $ git show #SHA-1.</p><p><strong>Summary: </strong>A good knowledge of git<em> tools</em> can help developers to debug the code with ease. This would make life happier.</p><p>Happy coding!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=20bf13318164" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Learning how references work in JavaScript]]></title>
            <link>https://medium.com/@naveenkarippai/learning-how-references-work-in-javascript-a066a4e15600?source=rss-bce54079a820------2</link>
            <guid isPermaLink="false">https://medium.com/p/a066a4e15600</guid>
            <category><![CDATA[reference]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[pointers]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Naveen Karippai]]></dc:creator>
            <pubDate>Sun, 13 Nov 2016 17:35:49 GMT</pubDate>
            <atom:updated>2023-01-14T09:58:34.459Z</atom:updated>
            <content:encoded><![CDATA[<p><strong><em>TL;DR</em></strong><em>:</em> There are NO pointers in JavaScript and references work differently from what we would normally see in most other popular programming languages. In JavaScript, it’s just NOT possible to have a reference from one variable to another variable. And, only compound values (Object, Array) can be assigned by <strong><em>reference</em></strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*HW_DdjA1Teer0qV0pxdkcw.jpeg" /><figcaption>src: <a href="https://memegenerator.net/">memegenerator</a></figcaption></figure><p><strong><em>Bottom line:</em></strong></p><ol><li>The<em> typeof </em>value assigned to a variable decides whether the value is stored with <em>assign-by-value</em> or<em> assign-by-reference</em></li><li>On variable assignment, the scalar primitive values (Number, String, Boolean, undefined, null, Symbol) are <em>assigned-by-value</em> and compound values (Object, Array) are <em>assigned-by-reference</em></li><li>The references in JavaScript only point at contained values and NOT at other variables or references</li><li>In JavaScript, scalar primitive values (Number, String, Boolean, undefined, null, Symbol) are <em>immutable</em> and compound values (Object, Array) are <em>mutable</em></li></ol><p><strong><em>A quick example of assign-by-value:</em></strong></p><p>In the code snippet below, we are assigning a scalar primitive value (number) to a variable and thus <em>assign-by-value</em> applies here. Firstly, the variable batman is initialized and when the variable superman is assigned with the value stored in the variable batman, it creates a new copy of the value and stores it. When the variable superman is modified, variable batman is left unaffected as they point to distinct values.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e0ba12630528a308aa4c9127299a2511/href">https://medium.com/media/e0ba12630528a308aa4c9127299a2511/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/614/1*QsD4yzqkNYgA40-m4mAgpg.png" /><figcaption>Tool: UMLet (GNU Linux client)</figcaption></figure><p><strong><em>A quick example of assign-by-reference:</em></strong></p><p>In the code snippet below, we are assigning a compound value (array) in a variable and thus <em>assign-by-reference</em> applies here. The variables flash and quicksilver are references to the same value (aka shared value). The references will point to the updated value when the shared value is modified.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5524844b97f239dafe7129cbe7520667/href">https://medium.com/media/5524844b97f239dafe7129cbe7520667/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/607/1*jsPkapD8J8q022ev6NVsHw.png" /><figcaption>Tool: UMLet (GNU Linux client)</figcaption></figure><p><strong><em>How to create a new reference?</em></strong></p><p>When the compound value in a variable is reassigned, a new reference is created. In JavaScript, unlike in most other popular programming languages, the references are pointers to values stored in variables and NOT pointers to other variables, or references.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2099e187ea1f6fcfda937874659cc404/href">https://medium.com/media/2099e187ea1f6fcfda937874659cc404/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/765/1*D3uCMwUX6xvZNSY4azfhLg.png" /><figcaption>Tool: UMLet (GNU Linux client)</figcaption></figure><p><strong><em>How do references work when values are passed as function parameters?</em></strong></p><p>In the code snippet below, the variable magneto is a compound value (Array object), thus it is assigned in the variable (function argument) x with assign-by-reference. The Array.prototype.push method invoked inside IIFE mutates the value in the variable magneto through JavaScript reference. But, the reassignment of the variable x creates a new reference, and further modifications on it do NOT affect the reference to the variable magneto.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e23641213e091b0adc38d42f4a40d1ae/href">https://medium.com/media/e23641213e091b0adc38d42f4a40d1ae/href</a></iframe><p><strong><em>How to change the original value in a compound variable passed as a function argument through JavaScript reference?</em></strong></p><p>The solution here would be to modify the existing compound value where the reference is pointing to. In the code snippet below, the variable wolverine is a compound value (Array object) and on IIFE invocation, the variable (function argument) x is assigned by reference. The Array.prototype.length property can be used to create an empty array by setting its value to 0. Thus, the variable wolverine is changed to the new value set in a variable x through JavaScript reference.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c3205e86ad6e356bce8793699eb0bb4c/href">https://medium.com/media/c3205e86ad6e356bce8793699eb0bb4c/href</a></iframe><p><strong><em>How to store a compound value through assign-by-value?</em></strong></p><p>The solution here would be to make a manual copy of the compound value and then assign the copied value to a variable. Therefore, the reference of the assigned value does NOT point back to the original value. The recommended approach to creating a (shallow) copy of the compound value (Array object) is to invoke Array.prototype.slice method on it with no arguments passed.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4c850d5193246595045dbc401e2da6c9/href">https://medium.com/media/4c850d5193246595045dbc401e2da6c9/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/652/1*2ADFn3r1_AH0CbW1YmFzlw.png" /><figcaption>Tool: UMLet (GNU Linux client)</figcaption></figure><p><strong><em>How to store a scalar primitive value through assign-by-reference?</em></strong></p><p>The solution here would be to wrap the scalar primitive value in a compound value (Object, Array) as its property value. Thus, it can be assigned-by-reference. In the code snippet below, the scalar primitive value in the variable speed is set as a property on the object flash. Therefore, it is assigned-by-reference on IIFE invocation to the variable (function argument) x.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d085a4f706424eda6e1dfc43a3bfa5a2/href">https://medium.com/media/d085a4f706424eda6e1dfc43a3bfa5a2/href</a></iframe><p><strong><em>An aside:</em></strong></p><p>In JavaScript, the scalar primitive values are <em>immutable</em> while compound values are <em>mutable</em>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/55d07137f811a84ee772dc4b24184ef0/href">https://medium.com/media/55d07137f811a84ee772dc4b24184ef0/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*G9y7ExBwqWnSEqAH4nov9Q.jpeg" /><figcaption>src: <a href="https://memegenerator.net/">memegenerator</a></figcaption></figure><p><strong><em>Summary: </em></strong>A good understanding of references in JavaScript can help developers to avoid many common mistakes and write better code.</p><p>Happy coding!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a066a4e15600" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Scoping and Hoisting in JavaScript]]></title>
            <link>https://medium.com/@naveenkarippai/scoping-and-hoisting-in-javascript-2c2e82107427?source=rss-bce54079a820------2</link>
            <guid isPermaLink="false">https://medium.com/p/2c2e82107427</guid>
            <category><![CDATA[scopes]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Naveen Karippai]]></dc:creator>
            <pubDate>Sat, 05 Nov 2016 18:39:30 GMT</pubDate>
            <atom:updated>2023-01-14T09:58:54.952Z</atom:updated>
            <content:encoded><![CDATA[<p>A scope can be defined as the context in which a variable exists. The scope can be local, or global in JavaScript.</p><p>Do you know the value logged on the JavaScript console when the following code snippet is run?</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/662ec099e785bac3243683e86cc04a65/href">https://medium.com/media/662ec099e785bac3243683e86cc04a65/href</a></iframe><p>The answer here is “The Flash”. This happens due to Function-level scoping in JavaScript on the variable declaration (using var keyword). Developers from a Java background may find this surprising!</p><h4>Function-level Scope</h4><p>In JavaScript, variables declared (using var keyword) are function-level scoped. The block-level scoping with loops and conditionals ( if,for, while, switch blocks) don’t delimit the scope.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6aee3dd369ace8a9f52d4a451ad963de/href">https://medium.com/media/6aee3dd369ace8a9f52d4a451ad963de/href</a></iframe><h4>Hoisting</h4><p>In JavaScript, hoisting is a powerful and expressive feature by which, the JavaScript interpreter moves the function and variable declarations to the top of the current referenced scope. The variable declarations and function declarations are hoisted in JavaScript!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d286b3e1f805e001530ddb69b68fd489/href">https://medium.com/media/d286b3e1f805e001530ddb69b68fd489/href</a></iframe><p>The function body is hoisted along with the function declaration. In the code snippet, the function declaration is moved to the top of the scope through hoisting (above return statement). The equivalent code interpreted would be:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d3d7d9754b385794b70cd9d015250c5e/href">https://medium.com/media/d3d7d9754b385794b70cd9d015250c5e/href</a></iframe><h4>An aside:</h4><p>The assignment part of variable and function expressions is <strong>not </strong>hoisted, but only the declarations are!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/30de5132206c26befcc2911a6daf8057/href">https://medium.com/media/30de5132206c26befcc2911a6daf8057/href</a></iframe><p>The equivalent code interpreted would be:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cedaa4610548b019428f93483b0bc024/href">https://medium.com/media/cedaa4610548b019428f93483b0bc024/href</a></iframe><h4>What’s new in ECMAScript 2015 (ES6)?</h4><p>ECMAScript 2015 introduced keywords: let and const for block-level scoping in JavaScript.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2793a46da29a75a323344447f6bf817b/href">https://medium.com/media/2793a46da29a75a323344447f6bf817b/href</a></iframe><p>The variables declared with let or const keyword has the block in which they are declared, as well as any continued sub-blocks as their scope.</p><h4>Hoisting in ES6</h4><p>In ECMAScript 2015, let and const keyword variable declarations will hoist the variable to the top of the block. However, referencing the variable in the block before variable declaration results in ReferenceError . The difference between var/function declarations and let/const declarations is in the initialization portion. The former are initialized with undefined or generator function right when binding is created at top of the scope, while the latter stay uninitialized until let/const statement is run. The variable is called to be in a “temporal dead zone” from the start of the block until the declaration is executed.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/90124f1c319e106768452ddd9698445d/href">https://medium.com/media/90124f1c319e106768452ddd9698445d/href</a></iframe><h4>variable declaration keywords: ‘var’ v/s ‘let’</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f252b026b18998637e9d45441027a3a7/href">https://medium.com/media/f252b026b18998637e9d45441027a3a7/href</a></iframe><p>This happens due to the block-level scoping of let variable declaration.</p><p><em>Summary</em>: Hoisting is a very powerful feature in JavaScript language. This provides high flexibility to the language if used necessarily. Also, a strong understanding of scoping in JavaScript can help us to avoid many common mistakes.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2c2e82107427" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>