I've done a fair bit of searching around to see if this is possible. So far, from what I understand, it isn't.
In my main.js
file, I have this code:
var commands = require('./commands.js');
It imports an object from commands.js
, that is set out in the following manner:
module.exports = {
var1: "something" + somevar,
var2: "something" + someothervar,
...
}
This allows me to access the object commands
from within the main.js
file as though it was just declaed within main.js
itself. However, I run into a problem when I want the exporting commands.js
file to be able to access main.js
's global variable scope. Say that somevar
and someothervar
are part of main.js
's global scope, when I want to get a value from commands
I don't want them to be 'undefined'.
Long story short, I want to treat this imported object exactly as though it was just declared in main.js
.
I'm trying to code a chatbot for Discord, and for the sake of tidyness and organization, I wanted to have the list of functions it can use separate from the code that processes them. Is this worth the effort? Is there a better way to do this? I'm also juggling around SQL at the moment, trying to design the most modular/customizable program possible.
This is not possible , when you export a module all the variables would have been resolved .
You will always get the object with all the expressions evaluated when you import
I would suggest make var1 and var2 as functions
module.exports = {
var1: function(someVar){ return "something" + someVar},
var2: function(someothervar) {return "something" + someothervar},
...
}
Now when you import it in main.js ,pass your somevar and someothervar
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.