Interview Questions & Answers

javascript

  • Module design pattern or the Module revealing pattern.

  • Design patterns are time tested architecture solutions.
  • So, to create a single instance we can use singleton pattern, if the object creational process is complex, we can use factory pattern and so.

  • A normal function has a name while IIFE does not have name.
  • So with a normal function you can have a name collision but with IIFE you will not have name, you will not have name collision.

  • IIFE solves the name collision problem.

  • Name collision happens when you have the same variable names or method names in the same context defined.

  • Name collision happens when you name the same variable names or method names in the same context.
  • <script>
      function Init(){
        var x =0;
      }
      var Init = 0;
      Init();
    </script>

  • IIFE is an anonymous function means it does not have name and it gets immediately invoked(it is self invoking).
  • <script>
        var x=0;
        (function(){
          var y=0
          alert(“I am IIFE and I will execute once when page loads”)
        })();
        alert(y);
    </script>

  • Closures are useful to create self contained code, functions that leads to a self contained state and we avoid global variables.