JavaScript with Oops is not much used by developers while creating application however we can make our life easy if we use JavaScript with Oops.
Let’s started object oriented programming in JavaScript by taking example of a Timer class, similar to the Timer control in windows application.
Output:
JavaScript Code: Create a Timer class with properties and methods
Create the class Timer and to add public properties and methods to the class we use this keyword as shown below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | <script> // Declaring class "Timer" var Timer = function() { // Property: Frequency of elapse event of the timer in millisecond this.Interval = 1000; // Property: Whether the timer is enable or not this.Enable = new Boolean(false); // Event: Timer tick this.Tick; // Member variable: Hold interval id of the timer var timerId = 0; // Member variable: Hold instance of this class var thisObject; // Function: Start the timer this.Start = function() { this.Enable = new Boolean(true); thisObject = this; if (thisObject.Enable) { thisObject.timerId = setInterval( function() { thisObject.Tick(); }, thisObject.Interval); } }; // Function: Stops the timer this.Stop = function() { thisObject.Enable = new Boolean(false); clearInterval(thisObject.timerId); }; }; </script> |
Example with Timer Class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | <!doctype html> <html> <head> <meta charset="utf-8"> <title>code4example.com</title> <script> // Declaring class "Timer" var Timer = function() { // Property: Frequency of elapse event of the timer in millisecond this.Interval = 1000; // Property: Whether the timer is enable or not this.Enable = new Boolean(false); // Event: Timer tick this.Tick; // Member variable: Hold interval id of the timer var timerId = 0; // Member variable: Hold instance of this class var thisObject; // Function: Start the timer this.Start = function() { this.Enable = new Boolean(true); thisObject = this; if (thisObject.Enable) { thisObject.timerId = setInterval( function() { thisObject.Tick(); }, thisObject.Interval); } }; // Function: Stops the timer this.Stop = function() { thisObject.Enable = new Boolean(false); clearInterval(thisObject.timerId); }; }; </script> </head> <body> <button id="b1">Start</button> <button id="b2">Stop</button> <div id="time"></div> <script> var b1=document.querySelector("#b1"); var b2=document.querySelector("#b2"); var timeDiv=document.querySelector("#time"); var counter=0; var timer = new Timer(); timer.Interval = 1000; b1.onclick=function(){ timer.Start(); } b2.onclick=function(){ timer.Stop(); } timer.Tick = timer_tick; function timer_tick() { timeDiv.innerHTML+="Passed:"+ (counter++) + "<br>"; } </script> </body> </html> |