In this constructor function I'm assigning everything to the proper variables. For the third parameter it accepts an options
argument that contains the optional settings.
To prevent a Cannot read property x of undefined.
error I'm always checking this variable by steps, since the options
variable can contain both complex objects and primitive data types.
I'm wondering if there is a more elegant way I could write this?
Constructor
function LiveDate(timeUrl, element, options) {
this.timeUrl = timeUrl;
this.element = element;
this.format = options && options.format ? options.format : LiveDate.formats.ISO8601;
this.offset = options && options.offset ? options.offset : 0;
this.weekdayNames = {
long: options && options.weekdayNames && options.weekdayNames.long ? options.weekdayNames.long : [
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday'
],
short: options && options.weekdayNames && options.weekdayNames.short ? options.weekdayNames.short : [
'Sun',
'Mon',
'Tue',
'Wed',
'Thu',
'Fri',
'Sat'
]
};
this.monthNames = {
long: options && options.monthNames && options.monthNames.long ? options.monthNames.long : [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
],
short: options && options.monthNames && options.monthNames.short ? options.monthNames.short : [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec'
]
};
this.start();
}
short
andlong
are reserved words. I.e. the parser must fail to parse this code (but many JavaScript parsers probably try to adjust for the mild errors in the input...) \$\endgroup\$