如果你想减少嵌套 if…else 或 switch case,你可以简单地使用基本的逻辑运算符AND/OR。
function doSomething(arg1){
arg1 = arg1 || 10;
// 如果尚未设置,则将 arg1 设置为 10 作为默认值
return arg1;
}
let foo = 10;
foo === 10 && doSomething();
// is the same thing as if (foo == 10) then doSomething();
// 输出: 10
foo === 5 || doSomething();
// is the same thing as if (foo != 5) then doSomething();
// 输出: 10













网友评论