今天无心间看到一篇文章(- -。忘记哪了..我大概说一下吧,原本能够直接分享的...),对于平时冗长的if else
的优化
. 平时也是这么处理的 经过object
对象的数据结构来实现优雅的判断条件书写! 可是看到经过map
数据结构的利用 感受适用更广,局限更低了 !一块儿来看看
Map
数据结构来作个简单介绍:定义:webpack
Map
对象保存键值对。任何值(对象或者原始值)
均可以做为一个键或一个值。语法:web
new Map([iterable])
Iterable
能够是一个数组或者其余iterable
对象,其元素或为键值对,或为两个元素的数组。 每一个键值对都会添加到新的Map
。null
会被当作undefined
。方法:数组
Map.prototype.get(key)
返回键对应的值,若是不存在,则返回undefined
。
Map.prototype.has(key)
返回一个布尔值,表示Map实例是否包含键对应的值。
Map.prototype.set(key, value)
设置Map对象中键的值。返回该Map对象。
对于Map数据结构来讲,不支持 = 号的赋值~~~~~~~
对于判断条件的单一
var status = 8; // 经常使用的if else 进行 条件判断来do somethings if(status == 1){ console.log(111111) }else if(status == 2){ console.log(222222) }else if(status == 3){ console.log(333333) }else if(status == 4){ console.log(444444) }else if(status == 5){ console.log(555555) }else{ console.log(status) } // 8 // switch case的写法 相比if else 是有一些优化了! switch (status){ case 1: console.log(status) break case 2: console.log(status) break case 3: console.log(status) break case 4: console.log(status) break case 5: console.log(status) break default: console.log(status) break; } // 8 // 对象object 数据结构的写法 简洁了 var obj = { "1":"11111", "2":"22222", "3":"33333", "4":"44444", "5":"55555", } console.log(obj[status] || status) // 8 // Map数据结构的写法 和object差很少 var mMap = new Map([ ["1","11111"], ["2","22222"], ["3","33333"], ["4","44444"], ["5","55555"] ]) console.log(mMap.get(status) || status) // 8
结果均可以达到预期的效果! 判断进行的顺利 ! 然而条件是个多个条件呢? 范围呢? 条件是个运算呢?
怎么实现? 接着看
var name = "lisi" , status = 1; //if else 写法 if(name == "lisi"){ if(status == 1){ console.log("lisi1") }else if(status == 2){ console.log("lisi2") }else if(status == 3){ console.log("lisi3") }else if(status == 4){ console.log("lisi4") }else if(status == 5){ console.log("lisi5") }else{ console.log(status) } }else if(name == "zhangsan"){ if(status == 1){ console.log("zhangsan1") }else if(status == 2){ console.log("zhangsan2") }else if(status == 3){ console.log("zhangsan3") }else if(status == 4){ console.log("zhangsan4") }else if(status == 5){ console.log("zhangsan5") }else{ console.log(status) } } //lisi1 //swtich case 写法 switch (status && name){ case 1 && "lisi": console.log(name + status) break ... default: console.log(status) break; } //lisi1 // 对象数据结构的写法 //简洁 var obj = { "lisi_1":"lisi1", "lisi_2":"lisi2", ... "zhangsan_5":"zhangsan5", } console.log(obj[name + "_" + status] || status) // lisi1 // Map数据结构的写法 和object差很少 var mMap = new Map([ ["lisi_1","lisi1"], ["lisi_2","lisi2"], ... ["zhangsan_5","zhangsan5"] ]) console.log(mMap.get(name + "_" +status) || status) //lisi1
多个条件也进行了对比,均可以完美实现,书写上相对于来讲更为简洁 固然可读性较低一点.. 性能差别确定也存在. 不过对于平日的基础业务能够忽略不计.接下来对于运算,范围
用Map
来实现一下 来了解一下~
var mMap = new Map([ [162,function(h,a){console.log("he height is" + h + " ,he age is" + a)}], [174,function(h,a){console.log("he height is" + h + " ,he age is" + a)}], [198,function(h,a){console.log("he height is" + h + " ,he age is" + a)}], ]) var height = 150, age = 12; mMap.get(height + age)(height,age) //he height is150 ,he age is12 //正则 var mMap = new Map([ [/^\d{2,5}$/,function(h,a){console.log("位数大于2且小于5")}], [/^\d{5,10}$/,function(h,a){console.log("位数大于5且小于10")}], ]) var arr = [...mMap].filter(([k,v])=>(k.test(`123`))) arr.forEach(([k,v])=>v.call(this)) //位数大于2且小于5
Map结构是支持任何对象任何原始值做为key|value的,因此大家能够开动大脑再试试其它,我就不介绍了.明白这样写就好
, 固然能够适当封装,可是这个业务代码耦合性略高,封装意义不大,此处就不作说明了!
有小伙伴看过那篇的能够评论区贴一下,(那篇文章篇幅比我长...比我确定全一些..)我只是简单的介绍了一下.平日都这么用.分享给你们.
关于
webpack后续
的文章 周一见 !