<?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: Muhammad Ridwan</title>
    <description>The latest articles on DEV Community by Muhammad Ridwan (@muhammadridwan).</description>
    <link>https://dev.to/muhammadridwan</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%2F87815%2F583e6381-8986-4028-8924-ce56623aa756.jpg</url>
      <title>DEV Community: Muhammad Ridwan</title>
      <link>https://dev.to/muhammadridwan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/muhammadridwan"/>
    <language>en</language>
    <item>
      <title>Namespace in JS</title>
      <dc:creator>Muhammad Ridwan</dc:creator>
      <pubDate>Sat, 01 Sep 2018 13:48:38 +0000</pubDate>
      <link>https://dev.to/muhammadridwan/namespace-in-js-5dbj</link>
      <guid>https://dev.to/muhammadridwan/namespace-in-js-5dbj</guid>
      <description>&lt;p&gt;Unfortunately JavaScript doesn’t provide namespace by default. So anything&lt;br&gt;
(function, method, object, variable) we create in JavaScript is global and &lt;br&gt;
we continue polluting that global namespace by adding more to that.&lt;/p&gt;

&lt;p&gt;JavaScript lack namespaces. However we can use Objects , IIFE  to create namespaces. &lt;/p&gt;

&lt;p&gt;Advantage of namespacing is that they organize JavaScript code, make JavaScript code maintainable, do not create unnecessary global variables and functions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem without namespace
&lt;/h2&gt;

&lt;p&gt;In this example we will define two functions that will share the same name. Have a look at the following example, we have defined fun1( ) two times and then we are calling fun1() and we are seeing that the latest function is executed.&lt;/p&gt;

&lt;p&gt;JavaScript-Demo&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    function fun1() {
        console.log("I am first fun1");

    }
    function fun1() {
        console.log("I am second fun1");
    }
    fun1();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Output:&lt;br&gt;
I am second func1&lt;/p&gt;

&lt;h2&gt;
  
  
  Using a namespace to solve the problem
&lt;/h2&gt;

&lt;p&gt;As we have explained earlier, a namespace solves the name collision problem. In this example we will share the same function name in more than one function but they will belong to different namespaces. Here look at the following two approaches:&lt;/p&gt;

&lt;h2&gt;
  
  
  1.Using Object Literal Notation
&lt;/h2&gt;

&lt;p&gt;Here we wrap variables and function inside Object literal which act as a namespace. We access wrapped variable and function through notation:&lt;/p&gt;

&lt;p&gt;object_name.variable_name;&lt;br&gt;
object_name.function_name();&lt;/p&gt;

&lt;p&gt;JavaScript-Demo&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   var myfunctionCollection1 = {
        fun1: function () {
              console.log("I am first fun1");             
        }        
   }
    var myfunctionCollection2 = {
        fun1: function () {
              console.log("I am second fun1");

        }
   }
    myfunctionCollection1.fun1();
    myfunctionCollection2.fun1();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Output:&lt;/p&gt;

&lt;p&gt;I am first fun1&lt;br&gt;
I am second fun1&lt;/p&gt;

&lt;h2&gt;
  
  
  2.Using IIFE (Immediately invoked function expression)
&lt;/h2&gt;

&lt;p&gt;An IIFE is an anonymous function contained within a pair of parenthesis and is invoked immediately. The pair of parenthesis creates a local scope for all the code inside of it and makes the anonymous function a function expression. This justifies the name“Immediately Invoked Function Expression”.&lt;/p&gt;

&lt;p&gt;The outermost pair of parenthesis turns everything inside of it to an expression because parentheses can’t contain JavaScript statements. The other pair of parentheses after function definition invokes the function immediately. Let’s look at an example.&lt;/p&gt;

&lt;p&gt;JavaScript-Demo&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   (function() {
   function fun1(){
   console.log("I am first fun1");
   } fun1();
   }());

   (function() {
   function fun1(){
   console.log("I am second fun1");
   } fun1();
   }());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Output:&lt;br&gt;
I am first fun1&lt;br&gt;
I am second fun1&lt;/p&gt;

&lt;h2&gt;
  
  
  3.Using a block and a let declaration (or a const declaration):
&lt;/h2&gt;

&lt;p&gt;In ES5, you had to use a pattern called IIFE (Immediately-Invoked Function Expression) if you wanted to restrict the scope of a variable to a block. In ECMAScript 6, you can simply use a block and a let declaration (or a const declaration):&lt;/p&gt;

&lt;p&gt;JavaScript-Demo&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; {
  let temp= function fun1(){
  console.log("I am first fun1");
  } 
  temp();
 }
  //temp(): ReferenceError: temp is not defined.

 {
  let temp= function fun1(){
  console.log("I am second fun1");
  } 
  temp();
 }
  //temp(): ReferenceError: temp is not defined.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Output:&lt;br&gt;
I am first fun1&lt;br&gt;
I am second fun1&lt;/p&gt;

&lt;p&gt;I hope you enjoyed this article.&lt;br&gt;
Regards.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
