更好的编写交换机的方法

时间:2015-11-03 19:38:49

标签: javascript

任何更短(或更有效)的方式来写这个。准确地说,每天,每周......都是从界面中提取的(输入[选择])

switch(repeatFrequency) {
                case "daily":
                    diffDays = 1;
                    break;
                case "weekly":
                    diffDays = 7;
                    break;
                case "weekly_2":
                    diffDays = 14;
                    break;
                case "monthly":
                    diffDays=31;//we assume the worst case
                    break;
                case "monthly_3":
                    diffDays = 92;//we assume the worst case
                    break;
}

1 个答案:

答案 0 :(得分:1)

更好的方法是使用hash(map)对象。像这样:

var hash = {
    'daily': 1,
    'weekly': 7,
    'weekly_2': 14,
    'monthly': 31,
    'monthly_3': 93,
};
var diffDays = repeatFrequency in hash? hash[repeatFrequency] : default_value;