3
\$\begingroup\$

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();
}
\$\endgroup\$
4
  • \$\begingroup\$ Technically, this shouldn't even work because short and long 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\$
    – wvxvw
    Commented Apr 8, 2015 at 13:13
  • \$\begingroup\$ @wvxvw Haven't experienced any problems with it in Chrome, FF nor IE. \$\endgroup\$ Commented Apr 8, 2015 at 13:14
  • 1
    \$\begingroup\$ Section 7.5.3 of ECMA-262 standard lists all future reserved keywords. It may be OK to use this now, but the standard explicitly tells not to do it because in the future they may be given semantics incompatible with your code. \$\endgroup\$
    – wvxvw
    Commented Apr 8, 2015 at 13:18
  • \$\begingroup\$ @wvxvw So technically it works, but it's just not recommended for the reason you just gave. \$\endgroup\$ Commented Apr 8, 2015 at 13:22

1 Answer 1

1
\$\begingroup\$

You could do something like this to clean things up a little:

options = options || {};

// Set to Default values first
this.weekdayNames = {
  "long": [...],
  "short": [...]
};

// Then check to see if we need to override
if ( options.weekdayNames ){
  for ( var key in options.weekdayNames ){
    this.weekdayNames[ key ] = options.weekdayNames[ key ];
  }
}

Furthermore, something like lodash's defaults function https://lodash.com/docs#defaults may be of use.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.