先看常規的條件語句的寫法:git
function getInitData(columnType){ if(columnType == "baiduZhidaoNum"){ return {searchText:"", href:""}; }else if(columnType == "weBo"){ return {weiboType:"xinlangWeibo",weiboUrl:""}; }else if(columnType == "APPDownload"){ return {downLoadType:"templateSetting"}; }else if(columnType == "contactPhone"){ return {phoneArray:""}; }else if(columnType == "inlineMap"){ return {address:"", latitude:"", longitude:""}; }else if(columnType == "weStore"){ return {weiStoreType: "taobaoStore", storeWebAddress:""}; }else if(columnType == "weRecruit"){ return {jobType:"weijob_zhaopinUrl", content:"", weijobUrl:""}; } }
拒絕if else以後大概是這樣:ui
/* 這裏使用了ES6的Arrow Function, var fun = ()=>({searchText:"", href:""}) 等價於: var fun = function(){ return {searchText:"", href:""} } 或: var fun = (arg1, arg2)=>({searchText: arg1, href:arg2}) 等價於: var fun = function(arg1, arg2){ return {searchText: arg1, href:arg2} } */ function getInitJumpPage(columnType){ return { "baiduZhidaoNum" : ()=>({searchText:"", href:""}), "weBo" : ()=>({weiboType:"xinlangWeibo",weiboUrl:""}), "APPDownload" : ()=>({downLoadType:"templateSetting"}), "contactPhone" : ()=>({phoneArray:""}), "inlineMap" : ()=>({address:"", latitude:"", longitude:""}), "weStore" : ()=>({weiStoreType: "taobaoStore", storeWebAddress:""}), "weRecruit" : ()=>({jobType:"weijob_zhaopinUrl", content:"", weijobUrl:""}), }[columnType]();