How to define and reference an array of CONSTANTS in deployed module #1197
Replies: 7 comments
-
Posted at 2018-09-24 by Wilberforce 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
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-09-24 by @gfwilliams @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:
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
Then you can access both:
OR: Maybe you just want both in one module? That's easy too:
Now you can use it like:
or
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-09-25 by Robin Mon 2018.09.24
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,
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:
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-09-25 by Robin 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? I've just been using 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? EDIT
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-09-25 by @gfwilliams
Yes, that's fine if you want just one class in a module.
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 I've seen in some of your code you do:
But in that case, |
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-09-26 by Robin Tue 2018.09.25 Thank you for clearing that up.
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. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-09-26 by @gfwilliams
Do you remember which one? That would be a bug in the module if so. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Posted at 2018-09-23 by Robin
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 deployedThought 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.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 );
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 -------------------------------------------- **Attachments:**
Beta Was this translation helpful? Give feedback.
All reactions