I am mentioning two approaches for creating the Singleton class in NodeJS express app, please help me in understanding few things about these implementations:
- Confirm if both mean the same - I guess yes, because results in same run time output
- Which one is preferred way of coding
- Is there any performance difference
- Any other better way of creating classes for Controllers and database wrappers in NodeJS Express app
Approach 1
(function (nameClass) {
nameClass.name = 'John';
nameClass.getName = function () {
return nameClass.name;
};
nameClass.setName = function (newName) {
nameClass.name = newName;
};
})(module.exports);
Approach 2
var fu = new function () {
this.name = "John";
this.getName = function () {
return this.name;
};
this.setName = function (newName) {
this.name = newName;
};
};
module.exports = fu;