I am currently working with NodeJS as the server side for a project and have created a 'module' for my internal helper functions.
Below is an example of how I currently set up my 'helper' file.
# Structure 1
var express = require('express');
var helpers = {
isNullOrEmpty: function(s)
{
return s == "" || s == null || s.length == 0;
},
formatDate: function(s)
{
var dateArr = s.split('/');
if (dateArr.length == 3)
{
return dateArr[1] + '-' + dateArr[0] + '-' + dateArr[2];
}
else
{
return null;
}
}
}
module.exports = helpers;
As you can see above, its setup 'object' like, and all works fine within the project and have had no issue with it, BUT when i see examples of people creating their own modules, they use the structure like below.
# Structure 2
var express = require('express');
function helpers(){};
helpers.protoype.isNullOrEmpty = function(s){
return s == "" || s == null || s.length == 0;
}
helpers.prototype.formatDate = function(s){
var dateArr = s.split('/');
if (dateArr.length == 3){
return dateArr[1] + '-' + dateArr[0] + '-' + dateArr[2];
}
else {
return null;
}
}
}
module.exports = helpers;
Is there a right or wrong way to set the structure of these files up? is there any advantage / disadvantage of formatting these modules?
Any information, 'best practice' or feedback would be most appreciated!
it comes from basic meanings of oop in javascript and in this case it becauses when you use object.prototype then you can define other objects that Inherit from parent object and all functions can use in child object for the inheritance in js you can use like this: