• Sun 2018.09.23

    Goal: Allow user to reference individual array constants by given group name reference in a deployed module

    User doesn't know the 'name' of a name=value pair, and needs to extract that value.
    User is given a group name and sub-group which is that 'name'

    Sample code works as expected in IDE editor window as values are of global scope. When deployed as modules however:

    Problem 1) Only the last 'exports=' statement is recognized, meaning there isn't a way to gain access to var C once deployed

    Thought I had an answer to that by deploying to two separate modules, but found out:

    Problem 2) It doesn't appear possible to embed or nest a require() statement inside another module in order to obtain a reference to the first defined set of constants, in order to define a group.

    var C = require("https://github.com . . .");
    
    var GROUPS = require("https://github.com . . .");
    

    The above wont work as the second file retrieved by var GROUPS needs the definitions that are in the first file in order to create the group array definition in the second file.

    Summary: User has no access to individual constants, in this case var C
    Has access to sub-group names in this case GROUPS.COMPASS
    ex: print( GROUPS.COMPASS.E );

    var exports={};
    
    var C = {
      U : "up",
      D : "down",
      L : "left",
      R : "right",
      N : "north",
      E : "east",
      S : "south",
      W : "west",
      T : "top",
      M : "middle",
      B : "bottom"
    };
    exports = C;
    
    
    var GROUPS = {
      COMPASS   : [{N:C.N}, {E:C.E}, {S:C.S}, {W:C.W}],
    //  COMPASS   : [ C.N, C.E, C.S, C.W ],  
      DIRECTION : [ C.U, C.D, C.L, C.R ],
      VERTICAL  : [ C.T, C.M, C.B ]
    };
    exports = GROUPS;
    
    
    var exports={};
    
    
    exports.C;
    exports.GROUPS;
    
    
    
    var C = require("https://github.com/sleuthware/E­spruinoDocs/blob/master/modules/moduleCo­nstantsTest.js");
    
    print( C.S );
    print( GROUPS.COMPASS );
    print( GROUPS.COMPASS.E );
    



    Constants need to be in separate module from group array labels which have their own module.
    Open to any option as design not cast in stone. Thanks for taking a looksie


    2 Attachments

  • Go back and have a look at the sample: http://www.espruino.com/Writing+Modules

    You need to add a constructor function and export the reference to that.

    The bind your contants to that function. so if you called it myMod :

    var myMod = function (options) {
      this.options = {}    || options;
    };
    
    
    myMod.C = {
      U : "up",
      D : "down",
      L : "left",
      R : "right",
      N : "north",
      E : "east",
      S : "south",
      W : "west",
      T : "top",
      M : "middle",
      B : "bottom"
    };
    
    exports = myMod;
    
    my_mod=new myMod();
    print my_mod.C.U;
    
  • @Wilberforce the constructor function isn't actually required as exports is normally just an object (but for most modules you would have one anyway so your method makes a lot of sense).

    The first bit of @Robin's code is a valid module:

    var C = {
      U : "up",
      D : "down",
      L : "left",
      R : "right",
      N : "north",
      E : "east",
      S : "south",
      W : "west",
      T : "top",
      M : "middle",
      B : "bottom"
    };
    exports = C;
    

    I'm not sure I understand the question @Robin but I think you're asking if you can reference one module from another one? Yes, you can.

    Assume you have the code above in one module https://github.com/.../C.js. The next module can be:

    var C = require("https://github.com/.../C.js");
    var GROUPS = {
      COMPASS   : [{N:C.N}, {E:C.E}, {S:C.S}, {W:C.W}],
    //  COMPASS   : [ C.N, C.E, C.S, C.W ],  
      DIRECTION : [ C.U, C.D, C.L, C.R ],
      VERTICAL  : [ C.T, C.M, C.B ]
    };
    exports = GROUPS;
    

    Then you can access both:

    var C = require("https://github.com/.../C.js");
    var GROUPS = require("https://github.com/.../GROUPS.j­s");
    print( C.S );
    print( GROUPS.COMPASS );
    print( GROUPS.COMPASS.E );
    

    OR: Maybe you just want both in one module? That's easy too:

    var exports={}; // you don't need this in the module itself. It's just when in the IDE
    var C = {
      U : "up",
      D : "down",
      L : "left",
      R : "right",
      N : "north",
      E : "east",
      S : "south",
      W : "west",
      T : "top",
      M : "middle",
      B : "bottom"
    };
    exports.C = C; //<------------------------------- Note the added '.C'
    var GROUPS = {
      COMPASS   : [{N:C.N}, {E:C.E}, {S:C.S}, {W:C.W}],
    //  COMPASS   : [ C.N, C.E, C.S, C.W ],  
      DIRECTION : [ C.U, C.D, C.L, C.R ],
      VERTICAL  : [ C.T, C.M, C.B ]
    };
    exports.GROUPS = GROUPS; //<------------------------------- Note the added '.GROUPS'
    

    Now you can use it like:

    var module = require(" ... ");
    print( module.C.S );
    print( module.GROUPS.COMPASS );
    print( module.GROUPS.COMPASS.E );
    

    or

    var module = require(" ... ");
    var C = module.C;
    var GROUPS = module.GROUPS;
    print( C.S );
    print( GROUPS.COMPASS );
    print( GROUPS.COMPASS.E );
    
  • Mon 2018.09.24

    re: 'need to add a constructor function'

    Thanks @Wilberforce, understand that and constructor isn't necessary, . . . found a different module to emulate, more to the design I have.

    Thank you @Gordon, you understood exactly, those examples clear it all up. I actually tried the nesting of a require() inside a deployed module,

    ref first code line 1. after: 'The next module can be:'

    but was stymied on an issue, causing me to create this post of a simplified version, but can't remember what was blocking progress.


    The key to understanding the missing part for the combined module:

    exports.C = C; //<------------------------------- Note the added '.C'

  • Tue 2018.09.24

    cont.

    While I re-attempt the above, too limited a time during week, I have a similar issue with establishing a class reference in a deployed module.

    Do I need the similar 'added C' as in 'exports.C = C;' to reference a class definition or do I need the constructor function to be part of the module? Is it bad practice to reference that class without the constructor function as part of the module?
    ( even though the constructor is defined inside the class definition )

    I've just been using exports = ClassName; which seems to be working fine. var cnInst = new ClassName();

    Is that above mentioned syntax the solution to referencing multiple classes within the same module? e.g. can more than one class exist in any one module?
    exports.ClassName = ClassName;
    exports.ClassNameDiff = ClassNameDiff;


    EDIT
    Ahhhh, . . . I think the solution is the last code block from #3 above. After

    'Now you can use it like:'

  • I've just been using exports = ClassName; which seems to be working fine.

    Yes, that's fine if you want just one class in a module.

    e.g. can more than one class exist in any one module? exports.ClassName = ClassName; exports.ClassNameDiff = ClassNameDiff;

    Yes, that's right. If you want two classes you can do it like that.

    Modules aren't particularly magic or clever - whatever is in exports after the module has executed is available when you then use require, so you treat it just like you would any other variable.

    I've seen in some of your code you do:

    exports = foo;
    exports = bar;
    exports = baz;
    

    But in that case, foo and bar get lost. After the code has executed, exports is equal to baz, so that's what gets exported.

  • Tue 2018.09.25

    Thank you for clearing that up.

    re: 'But in that case, foo and bar get lost.'

    I had seen that technique in another module and just blindly copied it, but always wondered if in fact there is only one exports instance.

  • I had seen that technique in another module and just blindly copied it

    Do you remember which one? That would be a bug in the module if so.

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

How to define and reference an array of CONSTANTS in deployed module

Posted by Avatar for Robin @Robin

Actions