<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          20條JavaScript代碼簡潔的寫法

          共 4050字,需瀏覽 9分鐘

           ·

          2021-12-24 19:26


          1、通過條件判斷給變量賦值布爾值的正確姿勢

          // badif (a === 'a') {    b = true} else {    b = false}
          // goodb = a === 'a'

          2、在if中判斷數(shù)組長度不為零的正確姿勢

          // badif (arr.length !== 0) {    // todo}
          // goodif (arr.length) { // todo}

          3、同理,在if中判斷數(shù)組長度為零的正確姿勢

          // badif (arr.length === 0) {    // todo}
          // goodif (!arr.length) { // todo}

          4、簡單的if判斷使用三元表達(dá)式

          // badif (a === 'a') {    b = a} else {    b = c}
          // goodb = a === 'a' ? a : c

          5、使用includes簡化if判斷

          // badif (a === 1 || a === 2 || a === 3 || a === 4) {    // todo}
          // goodlet arr = [1, 2, 3, 4]if (arr.includes(a)) { // todo}

          巧用數(shù)組方法,盡量避免用for循環(huán)

          6、使用some方法判斷是否有滿足條件的元素

          // badlet arr = [1, 3, 5, 7]function isHasNum (n) {    for (let i = 0; i < arr.length; i ++) {        if (arr[i] === n) {            return true        }    }    return false}
          // goodlet arr = [1, 3, 5, 7]let isHasNum = n => arr.some(num => num === n)
          // bestlet arr = [1, 3, 5, 7]let isHasNum = (n, arr) => arr.some(num => num === n)

          7、使用forEach方法遍歷數(shù)組,不形成新數(shù)組

          // badfor (let i = 0; i < arr.length; i ++) {    // todo    arr[i].key = balabala}
          // goodarr.forEach(item => { // todo item.key = balabala})

          8、使用filter方法過濾原數(shù)組,形成新數(shù)組

          // badlet arr = [1, 3, 5, 7],    newArr = []for (let i = 0; i < arr.length; i ++) {    if (arr[i] > 4) {        newArr.push(arr[i])    }}
          // goodlet arr = [1, 3, 5, 7]let newArr = arr.filter(n => n > 4) // [5, 7]

          9、使用map對數(shù)組中所有元素批量處理,形成新數(shù)組

          // badlet arr = [1, 3, 5, 7],       newArr = []for (let i = 0; i < arr.length; i ++) {       newArr.push(arr[i] + 1)}
          // goodlet arr = [1, 3, 5, 7]let newArr = arr.map(n => n + 1) // [2, 4, 6, 8]

          巧用對象方法,避免使用for...in

          10、使用Object.values快速獲取對象鍵值

          let obj = {      a: 1,      b: 2}// badlet values = []for (key in obj) {      values.push(obj[key])}
          // goodlet values = Object.values(obj) // [1, 2]

          11、使用Object.keys快速獲取對象鍵名

          let obj = {       a: 1,       b: 2}// badlet keys = []for (value in obj) {      keys.push(value)}
          // goodlet keys = Object.keys(obj) // ['a', 'b']

          巧用解構(gòu)簡化代碼

          12、解構(gòu)數(shù)組進(jìn)行變量值的替換

          // badlet a = 1,      b = 2let temp = aa = bb = temp
          // goodlet a = 1, b = 2[b, a] = [a, b]

          13、解構(gòu)對象

          // badsetForm (person) {      this.name = person.name       this.age = person.age}
          // goodsetForm ({name, age}) { this.name = name this.age = age }

          14、解構(gòu)時重命名簡化命名

          有的后端返回的鍵名特別長,你可以這樣干

          // badsetForm (data) {       this.one = data.aaa_bbb_ccc_ddd       this.two = data.eee_fff_ggg}// goodsetForm ({aaa_bbb_ccc_ddd, eee_fff_ggg}) {      this.one = aaa_bbb_ccc_ddd      this.two = eee_fff_ggg}
          // bestsetForm ({aaa_bbb_ccc_ddd: one, eee_fff_ggg: two}) { this.one = one this.two = two}

          15、解構(gòu)時設(shè)置默認(rèn)值

          // badsetForm ({name, age}) {      if (!age) age = 16       this.name = name      this.age = age }
          // goodsetForm ({name, age = 16}) { this.name = name this.age = age}

          16、||短路符設(shè)置默認(rèn)值

          let person = {       name: '張三',      age: 38}
          let name = person.name || '佚名'

          17、&&短路符判斷依賴的鍵是否存在防止報錯'xxx of undfined'

          let person = {       name: '張三',      age: 38,      children: {             name: '張小三'      }}
          let childrenName = person.children && person.childre.name

          18、字符串拼接使用${}

          let person = {      name: 'LiMing',       age: 18}// badfunction sayHi (obj) {      console.log('大家好,我叫' + person.name = ',我今年' + person.age + '了')}
          // goodfunction sayHi (person) { console.log(`大家好,我叫${person.name},我今年${person.age}了`)}
          // bestfunction sayHi ({name, age}) { console.log(`大家好,我叫${name},我今年${age}了`)}

          19、函數(shù)使用箭頭函數(shù)

          let arr [18, 19, 20, 21, 22]// badfunction findStudentByAge (arr, age) {      return arr.filter(function (num) {            return num === age       })}
          // goodlet findStudentByAge = (arr, age)=> arr.filter(num => num === age)

          20、函數(shù)參數(shù)校驗

          // badlet findStudentByAge = (arr, age) => {    if (!age) throw new Error('參數(shù)不能為空')    return arr.filter(num => num === age)}
          // goodlet checkoutType = () => { throw new Error('參數(shù)不能為空')}let findStudentByAge = (arr, age = checkoutType()) => arr.filter(num => num === age)

          本文完~




          學(xué)習(xí)更多技能

          請點擊下方公眾號

          瀏覽 42
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  操逼网123. | 国产一区久久 | 无码一区二区免费三区在线 | 欧美一级看片a免费观看 | 精品不卡一区二区三区 |