日期:2014-05-16 浏览次数:20409 次
js在开发大型组件库的时候经常会碰到很多的逻辑分支情况。
if(target === "font"){ someFunction(){...} } else if(target === "code"){ someFunction(){...} } else if(target === "table"){ someFunction(){...} } else if(target === "images"){ someFunction(){...} } else if(target === "link"){ someFunction(){...} } else if(target === "file"){ someFunction(){...} }
为了逻辑清晰当然也可以这样写:
switch(target){ case : "font" : someFunction(){...} break; case : "code" : someFunction(){...} break; case : "table" : someFunction(){...} break; case : "images" : someFunction(){...} break; case : "link" : someFunction(){...} break;
case : "file" : someFunction(){...} break;
}