Expose local variables defined in anonymous function to the global space - Javascript Data Type

Javascript examples for Data Type:Variable Scope

Description

Expose local variables defined in anonymous function to the global space

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){/*from   w  w  w .j a va2 s .c  o  m*/
var module = (function () {
    var foo = 123;
    var bar = function () {
      console.log('Hallo');
    };
    return { foo: foo, bar: bar }
})();
console.log(module.foo)
module.bar();
    }

      </script> 
   </head> 
   <body>  
   </body>
</html>

Related Tutorials