title: Why my singleton wont work?
body: <p>I'm beginner on JS, trying to understand how singleton works, and after reading much about so many singleton pattern, I conclude that the following singleton pattern would work,</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var singletonIns = (function() {
    var instance;
    function init() {
      var _pvtVar = 123;
      var pubVar = 111;
      var logPvt = function() {
        console.log(_pvtVar);
      };
      
      return {
        pubVar : pubVar,
        logPvt : logPvt
      };
    }
    return {
      getInstance : function() {
        if (!instance) {
          instance = init();
        } else {
          return instance;
        }
      }
    };
  })();</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>It does't throw any error on runtime</strong>.</p>
  
  <p>However, when i run this code <code>var abc = singletonIns.getInstance()</code> it returns <code>undefined</code>, and we cannot access <code>abc.pubVar</code>, <strong>why is that?</strong></p>
  
  <p>Please explained like I'm five yo.</p>
  
  <p>Thanks in advance.</p>