node.js expose variable to module?
I have read a lot of articles about how to create modules in node.js, and
you can use module.exports to expose module internals to the file which
includes it.. awesome!
How does this work the other way around? I'll use the following as an
example :-)
USER.JS
function User() {
this.property = 'value';
this.doSomething = function() {
var getStuff = mainFileFunction();
// do something with getStuff
}
module.exports = User;
MAIN.JS
var myArray = [];
myArray.push('something');
myArray.push('something else');
mainFileFunction() {
for(thing in myArray) {
return myArray[thing];
}
}
var u = new user();
log(u.property); <--- THIS IS EXPOSED, COOL!
u.doSomething(); <--- This will throw an error because mainFileFunction
is not defined in user.js :-(
If I were to move mainFileFunction to the user file, then it still
wouldn't work because the myArray array wouldn't be defined... and if I
were to move that over too, I wouldn't be able to use it in other
functions in main (which I want to) :-)
I'm sorry if I'm missing something really obvious here... What I want is
to expose the parts of my choosing from modules I include (module.export
works for that) but I also want to expose everything from the main file to
all the includes..
or just expose everything to everything? is that totally messy and horrible??
Just to explain what I am trying to do here... I want to have classes
defined in separate files, but I want to instantiate a bunch of them as
objects in the main file and store them in arrays.. I want the objects to
contain methods which can access arrays of the other object types.
Thanks guys! :-)
No comments:
Post a Comment