How to collect user defined functions in a liberary in nodjs?
This question already has an answer here:
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/
链接地址: http://www.djcxy.com/p/97018.html上一篇: 什么是“严格模式”,它是如何使用的?
下一篇: 如何在nodjs中收集用户自定义函数?