Using new key word to create object - Javascript Object

Javascript examples for Object:Constructor

Description

Using new key word to create object

Demo Code

ResultView the demo in separate window

<html>
   <head></head>
   <body> 
      <p>Counting with a local variable.</p> 
      <button type="button" onclick="myFunction()">Count!</button> 
      <p id="demo"></p> 
      <script>
function Add(x) {//from  w  w  w .  j av a  2 s.c  o m
   var counter = x;
   return function () {return counter += 1;}
};
var add = new Add(0);
function myFunction(){
  document.getElementById("demo").innerHTML = add();
}

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

Related Tutorials