I have several functions
but I dont know how collect user defined functions in liberary to reuse them in all around the nodeJs projec?
Asked
Active
Viewed 132 times
1

EramSa
- 128
- 1
- 10
-
Read some of the source code to see how "require(x)" works - I think you're wanting to do something similar. – theGleep Sep 12 '17 at 16:16
-
Hi @thegleep I'm newbie to programing. Thanks I will do it now. – EramSa Sep 12 '17 at 16:24
1 Answers
1
you could do something like this: In your preferred folder, create a file like user.js Then, define a class, E6 way :
class Users {
//if you need a constructor, but it's not mandatory
constructor(username,email,otherParamYouNeed){
this.username = username;
this.email = email;
this.otherParamYouNeed = otherYouNeed
}
//then organize your user functions there
userFunctionThatDoesSomething(){
//do what you need
}
userFunctionThatDoesAnotherThing(){
// do what you need
}
}
//then export the class
module.exports = {Users}
After, in the file in which you need to call those functions :
var {Users} = require ('/path/to/user.js');
//if you have constructor in your class
var user = new Users(username,email,otherParamYouNeed);
//if not
var user = new Users;
After that, you will be able to use the functions you declared in the class in the file you have required the class in, like :
user.userFunctionThatDoesSomething(etc..);
Have a look at https://www.sitepoint.com/object-oriented-javascript-deep-dive-es6-classes/

Ermanno Palmizio Salieri
- 173
- 15