Define scoped variable with nested function and Get scoped variable - Javascript Data Type

Javascript examples for Data Type:Variable Scope

Description

Define scoped variable with nested function and Get scoped variable

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  ww  w .  j av a 2  s.  co  m*/
var counter = 0;
var foo;

function myFunc() {
  this.foo = function() {
    counter = 1000;
  }
}
foo = new myFunc();
foo.foo();
document.getElementById('result').textContent = counter;

    }

      </script> 
   </head> 
   <body> 
      <div id="result"></div>  
   </body>
</html>

Related Tutorials