Inheritance / inclusion in JS files

Posted on
  • I tried to follow the Example , but in Espruino IDE it didn't really work.

    I tried the following:

    module moq_base.js

    var wifi = require( "Wifi" );
    function MoqBase(){};
    MoqBase.prototype.ssid;
    MoqBase.prototype.startPairing = function () {...}
    

    and then "inherit":

    var MoqBase = require( "moq_base" );
    Moq.prototype = new MoqBase();
    Moq.prototype.constructor = Moq; 
    function Moq( options ) {....}
    

    but it resulted in exceptions like

    Uncaught Error: Constructor should be a function, but is Object
    at line 5 col 21
    Moq.prototype = new MoqBase();

    I tried also to define a plain function inside the "base" module and then call it, but it also fails - function not found.

    So, are there any examples / tutorials on using JS-inheritance / inlcusion for ESP?
    Or, it shouldn't be used and copy & paste is my only true friend?

    Thanks!

  • In your module, you haven't exported anything - so the result of including it will be an empty object. Do:

    var wifi = require( "Wifi" );
    function MoqBase(){};
    MoqBase.prototype.ssid;
    MoqBase.prototype.startPairing = function () {...}
    exports = MoqBase;
    

    And then you should be off to a better start. However first, I'd make life simple for yourself by trying it all in one file.

  • Thanks, the exports did the trick!

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Inheritance / inclusion in JS files

Posted by Avatar for Injecteer @Injecteer

Actions