• For me, things do still not add up...

    ...and made me conclude that this is the one reason - co-concluding w/ @Gordon:

    re: 'which isn't actually in the code you pasted up'

    indeed... :[ - because if the IDE settings are installation default with sandbox set, then the following application code uploads just fine - because there is no issue in the module - and it runs as expected (after issuing run(); in the console):

    Module code (as in initially posted by @Robin in post #1 w/ semicolon (;) removed from 3rd line, since Espruinio IDE editor complains... :))

    // testLED.js - module
    function testLED(pin) {
      this.p = pin[0];
    }
    exports = testLED;
    testLED.prototype.testUR = function() {
      var nInt = 42;
        console.log( "inside testUR() " + nInt );
    };
    exports.create = function() {
      return new testLED([A5]);
    };
    

    Application code:

    // testLEDApp.js - uses module testLED.js in 'method mode'
    var testLED = require("testLED").create();
    
    var iId = null;
    function onInit() {
      iId = setInterval(function(){
        testLED.testUR();
      },1000);
    }
    
    function run() { onInit(); }  
    function stop() { if (iId) iId = clearInterval(iId); }
    

    Console output as expected:

     1v99 (c) 2018 G.Williams
    >
    =undefined
    >run()
    =undefined
    inside testUR() 42
    inside testUR() 42
    inside testUR() 42
    >stop()
    =undefined
    > 
    

    Above application code uses the method mode (I call it this way). It works because you added .create() plain function as property to the exports object which is the carrier to expose your exported .create() function. Since the exports is at the same time the constructor function (class) , I'm curious if it can be used in class mode as well:

    // testLEDApp2.js - uses module testLED.js in 'class mode'
    var testLED = new (require("testLED"))([B3]);
    var iId = null;
    function onInit() {
      console.log("testLEDApp2 (testLED module in class mode)");
      iId = setInterval(function(){
        testLED.testUR();
      },1000);
    }
    
    function run() { onInit(); }  
    function stop() { if (iId) iId = clearInterval(iId); }
    

    And guess what, it woks, as console output shows:

     1v99 (c) 2018 G.Williams
    >
    =undefined
    >run()
    testLEDApp2 (testLED module in class mode)
    =undefined
    inside testUR() 42
    inside testUR() 42
    inside testUR() 42
    >stop()
    =undefined
    > 
    

    ***Just make sure that precedence to the Espruino interpreter with parentheses to get the module first and then apply 'new' to it - object return by it . ***

    Also be aware that now an array with a pin (as first element) has to be passed in the constructor function with new to allow the constructor to work (and the upload not to fail, because - as said again - construction is executed on upload, since code is in level 0... and [sic: now rambling] I'm saying here again as said in many other places, that executing application code in Level 0 is 'bad' / calls for troubles, because it is executed and creates all kinds of havoc, timing, things not initialized when running disconnected, etc... therefore, the application code again in a more safe way, with execution of application code in onInit(){...} or triggered in onInit()) :

    // testLEDApp3.js - uses module testLED.js in 'class mode'
    var TestLED = require("testLED"); // get class (1st Upper)
    var testLED; // instance of TestLED (1st lowercase)
    
    var iId = null;
    function onInit() {
      console.log("testLEDApp2 (testLED module in class mode)");
      testLED = new TestLED([B3]);
      iId = setInterval(function(){
        testLED.testUR();
      },1000);
    }
    
    function run() { onInit(); }  
    function stop() { if (iId) iId = clearInterval(iId); }
    

    And example of how to test the module inline as instructed by espruino.com/Writing+Modules:

    // testLEDInlineTest.js
    
    // to emulate module access for local, inline testing
    var exports = {};
    
    // --- begin module code
    
    // testLED.js - module
    function testLED(pin) {
      this.p = pin[0];
    }
    exports = testLED;
    testLED.prototype.testUR = function() {
      var nInt = 42;
        console.log( "inside testUR() " + nInt );
    };
    exports.create = function() {
      return new testLED([A5]);
    };
    
    // --- end module code
    
    // --- begin application code
    
    // var TestLED = require("testLED"); // get class (1st Upper) // commented for inline testing
    var TestLED = exports; // instead of require, directly access what the module definition exports
    var testLED; // instance of TestLED (1st lowercase)
    var iId = null;
    function onInit() {
      console.log("testLEDInlineTest (testLED inline test)");
      testLED = new TestLED([B3]);
      iId = setInterval(function(){
        testLED.testUR();
      },1000);
    }
    
    function run() { onInit(); }  
    function stop() { if (iId) iId = clearInterval(iId); }
    
    // --- end application code
    
    // eoc --- end of code
    

    NB: I'm PICO, and I approve this post! ...eh: code! ;-)

About

Avatar for allObjects @allObjects started