开发者

reading optional attributes from optional setting in javascript

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 efficientl开发者_StackOverflow中文版y, either in javascript or in jquery.


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.


also possible jquery's extend:

function myTest(arg1, arg2, optionalsettings) {
   var settings = $.extend( {
     opt1 : val1
     , opt2 : val2}, optionalsettings || {})
...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜