1
我喜欢“跳字典”的概念,它包含key:values,其中的值是函数。不过,我不确定我是否应该喜欢这个概念。“跳字典”,切换语句,或如何做“跳字典”没有评估?
我想替换一个长列表,如果if-else if
陈述。 (我应该使用switch
语句?)
有没有一种方法来实现“跳字典”而不使用eval? (我已经看过这个为什么EVAL不好: Why is using the JavaScript eval function a bad idea? )
示例使用eval
function some_func(arg) { console.log('I am a some func') };
function find_my(arg) { console.log('we are looing for your ' + arg); };
var jump = {
'1' : 'some_func()',
'2' : 'find_my("cat")',
'3' : 'find_my("dog")'
}
eval(jump['3'])
eval(jump['2'])
eval(jump['1'])
感谢。
喜欢它。时间重构。谢谢! –