Create function to return a function, sharing private data - Javascript Object

Javascript examples for Object:Private Member

Description

Create function to return a function, sharing private data

Demo Code

ResultView the demo in separate window

<html>
   <head></head>
   <body> 
      <script>
    var testFn = function(testVal) {
      var test = testVal;//from   w  ww .  j  ava 2s .  c  o  m
      return {
        getVal: function() {
          return  test;
        }
      }
    };
    var ab = testFn (4);
    var ac = testFn (2);
    console.log(ab.getVal(),ac.getVal())

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

Related Tutorials