javascript如何减少if-else代码(短路运算/三元运算符)

2025年03月03日 建站教程

javascript如何减少if-else代码,下面web建站小编给大家介绍几种可以减少if-else代码的写法!

短路运算

let a = 1 || 2

三元运算符

const fn = (val) {
  return val ? 1 : 0
}

对象配置

let type = 'C'

let arr = {
    'A': 1,
    'B': 1,
    'C': 2,
    'D': 3,
    default: 0
}
console.log(arr[type]) //2

本文链接:http://so.lmcjl.com/news/24184/

展开阅读全文
相关内容