被调用模块 (a.js):
var WEB_APP={
userList: new Map()
};
WEB_APP.User = class User {
constructor() {
this.client_id = '';
this.user_name = '';
}
};
WEB_APP.hasClient = function(client_id) {
return this.userList.has(client_id);
};
WEB_APP.addClient = function(user) {
this.userList.set(user.client_id, user);
};
WEB_APP.getClients = function() {
return this.userList;
};
// Export all the things! *\('o')|
for(prop in WEB_APP) {
if(WEB_APP.hasOwnProperty(prop)) {
module.exports[prop] = WEB_APP[prop];
}
}
调用模块 (b.js):
var WEB_APP = require('./a.js');
var us = new WEB_APP.User();
us.client_id = connection.remoteAddress;
WEB_APP.addClient(us);
console.log(WEB_APP.getClients());