Refactored to a fancy global variable?

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật