开发者

jQuery merge (cut down) default plugin options, which is better?

开发者 https://www.devze.com 2023-03-11 04:09 出处:网络
Hi I am building a plugin which unfortunately has 146 options... What is the best way to merge (cut down) options.

Hi I am building a plugin which unfortunately has 146 options...

What is the best way to merge (cut down) options.

Sorry for my poor english.

Example:

Lets say we want to apply different color to two different elements.

Normal Default Options:

elemColor1:'black',
elemColor2:'red',

and use them like this:

elem1.css('color',options.elemColor1);
elem1.css('color',options.elemColor2);

or as array?

elemColor:['black','red'],

and use them like this:

elem1.css('color',options.elemColor[0]);
elem2.css('color',开发者_如何学运维options.elemColor[1]);

or as object?

elemColor:{color1:'black',color2:'red'},

and use them like this:

elem1.css('color',options.elemColor.color1);
elem2.css('color',options.elemColor.color2);

if passed as Array or Object we have the problem that if we set other parameters than default and we forget the second argument of elemColor.

elemColor:['green'],

then jquery plugin cannot read default second value (red).

Which is better(performance, smaller file size, e.t.c)?

Is there any other way???


There is no doubt that objects are easier to manage and won't cost you any significant performance in the long run. If you really have a lot consecutive options (color1, color2, color3, etc.), it may make sense to use an array as an object value. Just be sure to organize and comment well. Something like:

{colors: [
          '#fff', // element 1
          '#000', // 2
          '#ccc', // 3
          ...
         ]
}


I would definitely go with the array from the standpoint of the people using this plugin. That seems like the most logical way to use it. Then you can just deal with the issue stated yourself. In your init you can just check the length of the passed array and if it's too small, deal with that; fill the missing ones with the ones from the default or something.

0

精彩评论

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