• Try this:

    when using one file than

    • rename class
    • use correct constructor call

    and there you go :-)

    exports = {};
    
    class StopWatch {
    //      constructor() {
    //        var timeStart = 0;
    //        var timeStop = 0;
    //      }
        // If option == "auto" auto start
          constructor(option) {
            var timeStart = 0;
            var timeStop = 0;
            if( typeof option == "string" ) {
              if( option == "auto" )
                this.start();
    //            start();
            }
          }
        // Method start()
          start() { 
            this.timeStart = Date.now();
            this.timeStop = 0;
          }
        // Method stop()
          stop() { 
            this.timeStop = Date.now();
          }
          // Method duration()
          duration(){
            return this.timeStop-this.timeStart;
          }
    }
    exports = StopWatch;
    
    
    //stopWatch = new (require('stopWatch'))();
    //stopWatch = new (require('moduleStopwatch'))();
    //stopWatch = new (require('moduleTestConstructor')("auto"­))();
    
    stopWatch = new (StopWatch)("auto");
    
    
    function onInit(){
          stopWatch.start();
          setTimeout( function(){
            stopWatch.stop();
            console.log('duration in sec: ',
                    ( stopWatch.duration() / 1000 ).toFixed(2) );
          }, 1000);
    }
    setTimeout(onInit,1000);
    
    
    
    
  • Mon 2018.10.08

    Attachment from #1 codeTestConstructorExports.js demonstrates working monolithic file:

    Thank you @MaBe for your effort, but only works as a monolithic file. When deployed, still has the same effect, which allObjects pointed out.
    stopWatch = new (StopWatch)("auto"); is missing the 'requires' keyword for module deployment.


    'I see kind of a mixup of class vs instance method and the linking to module.'

    Yes, agreed. I didn't think the casing of the class definition made a difference, and we are correct. It doesn't. See attached files.

    'instance methods, which are only understood by instances.'

    As the instance is aware of it's properties, such as this.timeStart, shouldn't the class methods also be recognized then? this.start();

    'invokes the constructor with ES6 class definition'

    May I ask how you determined this was a ES6 flavor? When I checked the 'features' link #1 above, I see 1v96 I made every effort to avoid any feature that may have played a role with the current version of the parser/compiler.

    '. . . which is difficult to respond to'

    @allObjects, totally agree and is also a challenge to make sure the correct wording is used to describe along with the simplest of snippets to get that point across.


    2 Attachments

About

Avatar for Robin @Robin started