开发者

reading optional attributes from optional setting in javascript

开发者 https://www.devze.com 2023-01-04 05:18 出处:网络
What is the most efficient way of reading optional attributes in an optional setting argument. I\'m using something like:

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

精彩评论

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