views:

26

answers:

1

What is the most efficient way of reading optional attributes in an optional setting argument. I'm using something like:

f = func(var1, optionalsettings) {
    var2 = (optionalsettings === undefined ? 0 
         : (optionalsettings['var2'] == null ? 0 : optionalsettings['var2']));
};

But I've the feeling it can be done more efficiently, either in javascript or in jquery.

+2  A: 

You could do:

var var2 = (optionalsettings && optionalsettings['var2']) || 0;

First you check is optionalsettings exists. If it does, you try to return optionalsettings['var2']. If that fails, you fall back on the default value.

Kobi
Kobi