• FIRST Option: Single Module File

    Create a module - js file - with the following content:

    // Something.js in .../modules folder of IDE Settings-PROJECT
    var Something = function(name) {
      this.name = name;
    };
    Something.prototype.sayHello = function() {
      console.log("Hello, my name is " + this.name + "!");
    };
    exports = Something;
    

    To use the Something module, enter following code into edit pane of Espruino Web IDE, upload it, and then press BTN (equal to BTN1):

    var Something = require("Something"); // pull in class w/ require module
    var thisThing = new Something("ThisThing"); // create a 1st instance of class
    var thatThing = new Something("ThatThing"); // create a 2nd instance of class
    setWatch(function() { thisThing.sayHello(); // make 1st instance say hello...
      }, BTN1, { repeat:true, edge:"rising", debounce:50}); // ...on button press
    setWatch(function(){ thatThing.sayHello(); // make 2nd instance say hello...
      }, BTN1, { repeat:true, edge:"falling", debounce:50}); // ...on button release
    

    An almost perfect example you find developed in this conversation:
    Software Buttons - Many buttons from just one hardware button. @Gordon made even a change to the export part of the Espruino firmware to accommodate this approach in a straight forward way, which you find in this conversation: How to create a module that is a 'Class' - aka a function usable with new - or: How to make require() return a function and not an object.

About

Avatar for allObjects @allObjects started