当前位置: 首页 > Web前端 > HTML

实战-前端设计模式

时间:2023-03-28 11:49:07 HTML

的策略模式在业务中可能需要多次判断同一类型,判断条件不断累加constresult=type=>{if(type===1){return"Thisistype1"}elseif(type===2){return"Thisistype2"}elseif(type===3){return"Thisistype3"}}同上,虽然好像条件比较多,属于单条件,我们可以用策略模式简单改造一下,如下:constresult=type=>{constobj={1:{msg:"Thisistype1"}2:{msg:"Thisistype2"}3:{msg:"Thisistype3"}}returnobj[type].msg}