js 拒绝 if ... else
先看常规的条件语句的写法:
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之后大概是这样:
/* 这里使用了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]();
关键字:JavaScript, if语句
版权声明
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处。如若内容有涉嫌抄袭侵权/违法违规/事实不符,请点击 举报 进行投诉反馈!