I am currently refactoring an application that i built in JavaScript.
The application uses a starting hour and a total working hour count in order to construct a timetable for daily, weekly and monthly views. The starting hour and total working hour count are the same for all timetables
I considered using a pattern to initialise the Timetable and started reading up on how to proceed in doing so in Javascript as I am not too familiar with the language. My orginal initialisation function for the Timetable had the following signature:
FillTimeTable( startingTimeWeekDay, workinghourCountWeekDay );
Problem with this method is that for a different view of the timetable I have to call the other views with the same signature again.
FillWeeklyTimeTable( startingTimeWeekDay, workinghourCountWeekDay );
FillMonthlyTimeTable( startingTimeWeekDay, workinghourCountWeekDay );
If the working hours change I or the poor bugger maintaining my code would have to alter them at different locations within the code and due to the way I organised my code they are not placed as neatly as here. So during refactoring I considered alternatives like a global variable, Singleton or an Object.
I read about objects, private members and constructors and decided that this might be the solution after reading up on Singleton Implementations and binning the global variables.
So currently I have the following code:
function Timetable( startingTimeWeekDay, workinghourCountWeekday) {
this.startWeekday = startingTimeWeekDay;
this.hoursWeekday = workinghourCountWeekday;
}
Timetable.prototype.startWeekday = function() {
return this.startWeekday;
}
Timetable.prototype.hoursWeekday = function() {
return this.hoursWeekday;
}
var myTimetable = new Timetable( 6, 8 );
Everything is working fine so far, but myTimetable just seems like a really fancy way of declaring a global variable to me.
Is this the case?
Did I just decide on the wrong strategy or is this the way to go or have I missed something essential to working with JavaScript?
4
If the application — or rather the instance of the application in a particular browser while it is hosting that page — has only one sense of “starting hour” and one sense of “total working hour count” that it shares throughout itself, it’s not a wrong thing to have each of those things represented by a single variable in the script. Scattering it all over the place would be worse.
This in turn means that you’re either going to naturally have global variables or a unique object that holds that state. From a pattern-theoretic sense, a unique object that is injected into the consumers of the object (either by setting a property on that object or via a construction argument) is best, but for small applications there’s no need to get that complex; global variables or a “well-known” global holder object can do just as well. (If the code is small, refactoring it when it grows won’t be a big chore. If the code is large, you use the fancier style to start with.)
I suggest that you create an object to hold the work schedule information, then pass the object to your Timetable functions.
var companyXWorkSchedule = {
"startingTimeWeekDay": 6,
"workinghourCountWeekday": 8
};
You don’t need to create a global variable. companyXWorkSchedule
could be a property on an object.
edit: added missing equals sign
3