以前写switch:
switch ( foo ) {
case "bar": alert("the value was bar -- yay!"); break;
case "baz": alert("boo baz :("); break;
default: alert("everything else is just ok");
}
现在可以用对象进行模拟:
var stuffToDo = {
"bar" : function() { alert("the value was bar -- yay!"); },
"baz" : function() { alert("boo baz :("); },
"default" : function() { alert("everything else is just ok"); }
};
if ( stuffToDo[ foo ] ) {
stuffToDo[ foo ]();
}else {
stuffToDo["default"]();
}