File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed
Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change 2121 * the cases matched.
2222 */
2323
24+ //antipattern
2425 var inspect_me = 0 ,
2526 result = '' ;
2627 switch ( inspect_me ) {
3435 result = "unknown" ;
3536 }
3637
38+ //preferred 1,
39+ //'cause it is much faster and easy to reuse and read
40+ //avoid to use switch
41+ var results = {
42+ 0 : "zero" ,
43+ 1 : "one"
44+ } ;
45+
46+ result = results [ inspect_me ] || "unknown" ;
47+
48+ //preferred 2
49+ var resultFuncs = {
50+ 0 : function ( ) {
51+ //some code for "zero" case
52+ return "zero" ;
53+ } ,
54+ 1 : function ( ) {
55+ //some code for one case
56+ return "one" ;
57+ }
58+ } ;
59+
60+ result = ( resultFuncs [ inspect_me ] || function ( ) {
61+ //some code for default case
62+ return "unknown" ;
63+ } ) ( ) ;
64+
3765 // References
3866 // http://net.tutsplus.com/tutorials/javascript-ajax/the-essentials-of-writing-high-quality-javascript/
3967 </ script >
You can’t perform that action at this time.
0 commit comments