这个怎么样:
var converters = {
'SIToImperial' : {
'cm' : function(val) {
// implementation, return something
},
'kg' : function(val) {
// implementation, return something
} //, etc.
},
'SIToUS' : {
'cm' : function(val) {
// implementation, return something
},
'kg' : function(val) {
// implementation, return something
} //, etc.
},
'USToSI' : {
'cm' : function(val) { /* ... */ } // etc
} // , etc
}
SIToSomething(value, unit, system) {
return converters["SITo" + system][unit](value);
}
var meterInImperial = SIToSomething(100, 'cm', 'Imperial');
var fiftyKilosInUS = SIToSomething(50, 'kg', 'US');