I have been converting some old javascript for use in a node.js module and it got me thinking about module pattern options.
I have seen a number of structures using exports. module.exports and even prototype, and it has left me wondering which method is considered best practice and why?
Here is a cut down module example from my code written in two ways.
Option 1:
var Helper = function() {};
Helper.latin_map = {"Á":"A","Ă":"A","Ắ":"A","Ặ":"A","Ằ":"A"};
Helper.prototype.urlise = function(orgString){
var lower = orgString.toLowerCase();
var latinised = lower.replace(/[^A-Za-z0-9\[\] ]/g, function(a) {
return Helper.latin_map[a] || a;
});
return latinised.replace(/\s+/g, '-')
}
module.exports = new Helper();
Option 2:
var latin_map = {"Á":"A","Ă":"A","Ắ":"A","Ặ":"A","Ằ":"A"};
module.exports = {
urlise : function(orgString){
var lower = orgString.toLowerCase();
var latinised = lower.replace(/[^A-Za-z0-9\[\] ]/g, function(a) {
return latin_map[a] || a;
});
return latinised.replace(/\s+/g, '-')
}
}
This is quite a simple example, but I will be extending this to provide several accessible functions within the same module, so before I allow things to get too complicated. I thought I would seek some advice on which approach is considered best practice.