How to collect user defined functions in a liberary in nodjs?
本问题已经有最佳答案,请猛点这里访问。
我有几个
你可以这样做:在首选文件夹中,创建类似user.js的文件然后,定义一个类,e6方式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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} |
之后,在需要调用这些函数的文件中:
1 2 3 4 5 | 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; |
之后,您将能够使用您在所需类的文件中在类中声明的函数,例如:
1 | user.userFunctionThatDoesSomething(etc..); |
查看https://www.sitepoint.com/object-oriented-javascript-deep-dive-es6-classes/