需求:基于颜色打印水果
switch...case
function printFruits(color){
switch (color) {
case 'red':
return ['apple']
break;
case 'yellow':
return ['banana']
break;
default:
return []
break;
}
}
let res = printFruits('red')
console.log(res)
对象字面量0 - 建议
const fruitsColor = {
red: ['apple'],
yellow : ['banana']
}
function printFruits2(obj, color){
return obj[color] || []
}
console.log(printFruits2(fruitsColor, 'yellow'))
console.log(printFruits2(fruitsColor, null))
map1 - 建议
const fruitsColorMap = new Map().set('red',['apple']).set('yellow', ['banana'])
function printFruits3(obj, color){
return obj.get(color) || []
}
console.log(printFruits3(fruitsColorMap, 'red'))
console.log(printFruits3(fruitsColorMap, null))