Currently I'm working on my private project(for learning) which is a simple To Do List. I'm trying to use the modular pattern (revealing module pattern, to be specific). The image below shows ma general idea how I'm going to build it.
Image of how my app looks like
So each module will be in a separate js file where each module looks like:
var TaskModule = (function () {
function someFunction(parameter) {
tasks = newTasks;
}
}
And there is the question: what if we want to create something like separate file with lets say 'helpers function'. It's not comfortable writing in each module something like:
var someElement = document.getElementById('id')
I have my helper function (this function is just an example):
var someElement = byId('id);
Of course I can create another module in the same way like the others but then I will have to call my function this way:
var someElement = HelpersModule.byId('id');
Which causes that my help function doesn't make sense anymore, it's not shorter than original version with document.getElementById. So to avoid writing this prefix 'HelpersModule' every time I call my helper function, I designed my HelpersModule like this:
(function(window) {
window.byId = function (selector, scope) {
return (scope || document).getElementById(selector);
};
})(window);
Now everything works fine but the way I create HelpersModule is not consistent with the others modules. Is it a bad practice to create modules in differents way or it's completely OK?
Aucun commentaire:
Enregistrer un commentaire