Add private members to a function - Javascript Object

Javascript examples for Object:Private Member

Description

Add private members to a function

Demo Code

ResultView the demo in separate window

<!--//  w ww  .  j a  v a2 s  .  c o  m
Created using JS Bin
http://jsbin.com
Released under the MIT license: http://jsbin.mit-license.org
-->
<html>
   <head></head>
   <body> 
      <script>
var obj = (function() {
  var a = 0;
  return {
    run: function () {
      var q = a;
      a += 1;
      return q;
    }
  };
})();
console.log(obj.run());
console.log(obj.run());

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

Related Tutorials