• IMNSHO, using object oriented (oo) terms is not a matter of code snippets... it's a matter of communicating apples and oranges... less talking about comparing them.

    A good introductory start is http://www.academia.edu/download/3216960­1/Object-Oriented_Software_Construction.­pdf - explains the concepts and the various language bindings.

    1400+ page is a very looooong start. So let's get a shorter one - 2 clips:

    1. https://medium.com/the-renaissance-devel­oper/python-101-object-oriented-programm­ing-part-1-7d5d06833f26 - intro into Class, Object (Instance), ...
    2. https://medium.com/the-renaissance-devel­oper/python-101-object-oriented-programm­ing-part-2-8e0db3ddd531 - intro into Encapsulation, Inheritance

    To bad that it is Python, but replacing self with this, _init_ with constructor and leaving out def, it is pretty close to JavaScript... after all, Python uses a lot (of the same as or) from JavaScript.

    And there is MDN with https://developer.mozilla.org/en-US/docs­/Web/JavaScript/Guide/Working_with_Objec­ts - at times confusing, because it shows all the options of oo pre-ES6 and ES6+ (and the ES6 modules aren't there yet... but the node.js).

    Now we do some changes to any code and discuss it...

    The shortest way though is sneak peek into the - or one of the best practice - solutions, like peeking at the solution for the Sudoku challenges in an airline magazine.

    Here the solutions, clear and straight, with all bells and whistles, and naming ad casing as best practices suggest (single saves and run... bulls-eye):

    // StopWatch 'class' as module "StopWatch.js" in modules folder
    class StopWatch {
          // constructor - class' (class) Method 'new' (instance)
          constructor(option) {
            var timeStart = 0;
            var timeStop = 0;
            if (option == "autostart")
               this.start();
          }
          // (instance) Method start()
          start() {
            if (    (this.timeStart === 0 && this.timeStop === 0)
                 || (this.timeStart !== 0 && this.timeStop !== 0)
               ) {
              this.timeStart = Date.now();
              this.timeStop = 0;
            } else {
              throw "IllegalStateException - stopwatch ticking.";
            }
          }
          // (instance) Method stop()
          stop() {
            if (this.timeStart !== 0 && this.timeStop === 0) {
              this.timeStop = Date.now();
            } else {
              throw "IllegalStateException - stopwatch not ticking.";
            }
          }
          // (instance) Method duration()
          duration(){
            if (this.timeStart !== 0 && this.timeStop !== 0) {
              return this.timeStop-this.timeStart;
            } else {
              throw "IllegalStateException - stop did not tick.";
            }
          }
    }
    exports  = StopWatch;
    
    // Application (project) using StopWatch class going as
    // stopWatchApp.js file into projects folder.
    var StopWatch = require('StopWatch'); // get class
    var stopWatch1 = new StopWatch(); // create instance
    function onInit(){
          stopWatch1.start();
          setTimeout( function(){
            stopWatch1.stop();
            console.log('stopWatch1: duration in sec: ',
                    ( stopWatch1.duration() / 1000 ).toFixed(2) );
          }, 1000);
          var stopWatch2 = new StopWatch("autostart");
          setTimeout( function(){
            stopWatch2.stop();
            console.log('stopWatch2: duration in sec: ',
                    ( stopWatch2.duration() / 1000 ).toFixed(2) );
          }, 2000);
          var stopWatch3 = new StopWatch("autostart");
          try {
            stopWatch3.start();
          } catch(x) {
            console.log('stopWatch3: start() exception: ',x);
          }
          try {
            console.log(stopWatch3.duration());
          } catch(x) {
            console.log('stopWatch3: duration() exception: ',x);
          }
    }
    setTimeout(onInit,1000);
    

    Output attached as screenshot.

    Notice the time sequence... StopWatch instance 3 gets bothered before instance 1 and 2 stop and show the duration.

    Instead of getting the class (line 3) and re-use it with all the new, one could require the class for every new (with not much performance penalty): ... = new (require("StopWatch"))() and ... = new (require("StopWatch"))("autostart"). Instead of throwing simple text exception, Error or RYO Exception class or object in literal notation or JSON could be used.


    1 Attachment

    • stopWatchAppConsoleOutput.png
About

Avatar for allObjects @allObjects started