My ECMA5 Proposal : make switch useful

The other day on jQuery forum, I spotted a code like this one:

jQuery('a').each(function(){
  switch(x) {
    case   1: $(this).css({ color: 'blue' }); break ;
    case   2: $(this).css({ color: 'red' });  break ;
    default : $(this).css({ color: 'green' });
 }
});

Unfortunately, for some, this is “too many keystrokes” … And peculiarly (then very young and rushed) Mr B. Eich forgot to make result of the switch available, so this is not possible in javascript :

$(this).css({ color:
switch(x) {
case   1: 'blue' ; break ;
case   2: 'red'  ;  break ;
default : 'green' ;
}
}) ;

Unless we get to the return value of the switch, with eval(), like this :

$(this).css({ color:
 eval(
 'switch(x){  case 1: "blue"; break;  case 2: "red"; break;  default: "green" } '
 )
} ) ;

So the switch statement after all returns the value, but it is not an legal ‘rvalue’ in JavaScript, at the moment. But this eval() trick is not very usefull and looks somewhat clumsy. So some have concluded they “must help” everyone with function similar to this :

//return the value if x matches the case
//case and value must be in pairs
//the last argument if given is the default value
//
//cond( input, case1, value1, case2, value2, ..... , value_for_default )
//
//example :
//
cond( x, 1, "blue", 2, "red", /*default is*/ "green" ) ;
//

Now the excitement is high indeed, because one can write this :

$(this).css({ color:
   cond( x, 1, "blue", 2, "red", /*default is*/ "green"
} ) ;

Where the cond function is this:

//last argument is default value
function cond( v ) {
    var j = 1 ;
    for ( ; j < arguments.length; j+=2 ) {
       if ( cond.cond(v,arguments[j]) ) return arguments[j+1];
    }
    return (!arguments[j-2]) ? undefined : arguments[j-2] ;
}
//allow users to change the condition operator used to match the value given
//default condition is 'equality', aka 'exact match'
cond.cond= function (a,b) { return (a) === (b) ; }

Example

cond( 1, 2, "apples", 1, "pears", "none" )
/*
returns: "pears"
*/

For some this is “Awesome”, while for others this is “obfuscation” . Personaly, I vote for ECMA5 to simply allow  assignment with the ‘switch’ statement, make the return value of the switch statement available for assignment :

//My ECMA5 proposal
var color = switch(x) {
  case 1: "blue"; break case 2: "red"; break default: "green"
} ;
//make this possible

–DBJ
Update 2010.Jan.10
Ben Alman made this idea into an useful jQuery plugin, here : http://benalman.com/projects/jquery-cond-plugin/

This entry was posted in Development, IT, javascript and tagged , . Bookmark the permalink.

One Response to My ECMA5 Proposal : make switch useful

  1. Dusan says:

    Update
    Interesting (probably for some readers) is that this cond() function could replace each and every switch() and of course if … then … else … if … else …, “cascades” , which obviously will lead to a more compact code. But not necessarily a faster one.
    Although I have not tought yet about optimising the cond() function …

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>