I read a lot of articles on how to create modules in node.js, and you can use module.exports to expose the module internals to a file that includes it .. awesome!
How does it work differently? As an example, I will give the following :-)
USER.JS function User() { this.property = 'value'; this.doSomething = function() { var getStuff = mainFileFunction();
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 moved mainFileFunction to a user file, then it still wouldn't work, because myArray would not be defined ... and if I moved it too, I would not be able to use it in other functions basically (what I want) :-)
I apologize if I am missing something really obvious here. I want to expose parts of my choice from the modules that I turn on (module.export works for this), but I also want to expose everything: the main file for everyone includes.
or just expose everything to everything? Is it absolutely messy and terrible?
Just to explain what I'm trying to do here ... I want classes to be defined in separate files, but I want to instantiate them as objects in the main file and store them in arrays .. I want objects containing methods that can access arrays of other types of objects.
Thanks guys!: -)
source share