简体   繁体   中英

Cleaning up repititious jQuery in a plugin and making sure performance is as good as it can be

I've completed writing a purpose-specific plugin and there are a few spots where I have duplicate code and due to the structure of the function, I'm not quite sure how to cleanly get rid of repetitious code.

You can see the code here:

http://jsbin.com/aboca3/8/edit

I am specifically referring to the 4 switch statements and wondering how I can consolidate them in to a function in a way that makes sense, and more importantly, actually works.

Last but certainly not least, there may be more efficient ways to go about performing the functions I'm performing so if anyone has any input on that, I'd be very grateful.

Well those switch statements repeat almost the exact same code. Why not replace them with an object that you use to look up which sort function to call?

 var sorts = {
   'alpha': sortAlpha, 'em': sortEm, 'rel': sortRel
 };

then you can just call

 $('whatever')
   .sort(sorts[o.startingsort])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM