Node.js to set a variable in the module?

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(); // 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 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!: -)

+4
source share
4 answers

You can use global variables or have the correct circular dependency ( require with both files), however - this is usually a bad habit, which can lead to problems with maintainability in the future.

Instead, you can use dependency injection and insert doSomething into your module. It basically gives you the following for free:

  • You can test User with a simple doSomething mock implementation later and verify the code is correct
  • User dependencies are explicit and implicit, which makes it obvious what the user needs.

I will present two implementations: one using constructor dependency injection, and one with module configuration.

  USER.JS function User(dependentFunction) { this.property = 'value'; this.doSomething = function() { var getStuff = dependentFunction(); // do something with getStuff } } module.exports = User; MAIN.JS ... var u = new User(mainFileFunction); u.doSomething(); // this will now work, using mainFileFunction 

What happens here is pretty simple, and we know what happens.

It can also be set to the width of the module.

 USER.JS function User(depFunc) { this.property = 'value'; this.doSomething = function() { var getStuff = depFunc(); // do something with getStuff } } function UserFactory(depFunc){ return function(){ return new User(depFunc); } } module.exports = UserFactory; MAIN.JS var getUser = UserFactory(mainFileFunction); var u = getUser(); // will return a new user with the right function 
+7
source

+1 for Benjamin to respond to an injection of addiction. I would like to add another way to inject objects into your modules by passing the dependency to require, for example require ('./module.js') (dependFunction);

 //MAIN.js var db = function() { this.rows = []; this.insert = function(name) { this.rows.push(name); console.log('Db: Inserting user with name ' + name); } this.getAll = function(){ return this.rows; } } var fakeDb = new db(); var user = require('./user.js')(fakeDb); user.add('Jhon'); user.add('Rose'); user.list(); 

and

 //users.js module.exports = function(db) { return { add: function(name) { db.insert(name); }, list: function() { var users = db.getAll(); var i = users.length; console.log('listing users\n-------'); while(i--) { console.log(users[i]); } } } } 
+1
source

You must pass mainFileFunction as a parameter to the user constructor.

 USER.JS function User(mainFileFunction) { this.property = 'value'; this.doSomething = function() { var getStuff = mainFileFunction(); // do something with getStuff } module.exports = User; 

In your main.js use the following

var u = new user(mainFileFunction);

0
source

How about moving mainFileFunction in user.js and the function takes an array as an argument:

 mainFileFunction(array) { for(thing in array) { return array[thing]; } } 

And then when you call it from main.js, pass the function to your array:

 u.doSomething(myArray); 
0
source

Source: https://habr.com/ru/post/1499227/


All Articles