1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        8種ES6中擴(kuò)展運(yùn)算符的用法

        共 3763字,需瀏覽 8分鐘

         ·

        2021-07-05 02:16

        擴(kuò)展操作符 … 是ES6中引入的,將可迭代對(duì)象展開(kāi)到其單獨(dú)的元素中,所謂的可迭代對(duì)象就是任何能用for of循環(huán)進(jìn)行遍歷的對(duì)象,例如:數(shù)組、字符串、Map、Set、DOM節(jié)點(diǎn)等。

        1、拷貝數(shù)組對(duì)象

        使用擴(kuò)展符拷貝數(shù)組是ES6中常用的操作:
        const years = [2018, 2019, 2020, 2021];const copyYears = [...years];
        console.log(copyYears); // [ 2018, 2019, 2020, 2021 ]

        擴(kuò)展運(yùn)算符拷貝數(shù)組,只有第一層是深拷貝,即對(duì)一維數(shù)組使用擴(kuò)展運(yùn)算符拷貝就屬于深拷貝,看下面的代碼:

        const miniCalendar = [2021, [1, 2, 3, 4, 5, 6, 7], 1];
        const copyArray = [...miniCalendar];console.log(copyArray); // [ 2021, [ 1, 2, 3, 4, 5, 6, 7 ], 1 ]
        copyArray[1][0] = 0;copyArray[1].push(8);copyArray[2] = 2;console.log(copyArray); // [ 2021, [ 0, 2, 3, 4, 5, 6, 7, 8 ], 2 ]console.log(miniCalendar); // [ 2021, [ 0, 2, 3, 4, 5, 6, 7, 8 ], 1 ]

        把打印的結(jié)果放在一起便于更加清楚進(jìn)行對(duì)比,如下:

        變量說(shuō)明結(jié)果操作
        copyArray[ 2021, [ 1, 2, 3, 4, 5, 6, 7 ], 1 ]復(fù)制數(shù)組 miniCalendar
        copyArray[ 2021, [ 0, 2, 3, 4, 5, 6, 7, 8 ], 2 ]1. 將數(shù)組第二個(gè)元素的第一個(gè)元素重新賦值為 0 ;2. 往數(shù)組的第二個(gè)元素增加一個(gè)元素 8 ;3. 將數(shù)組第三個(gè)元素重新賦值為2
        miniCalendar[ 2021, [ 0, 2, 3, 4, 5, 6, 7, 8 ], 1 ]從結(jié)果來(lái)看,數(shù)組的第二個(gè)元素為數(shù)組,大于1維了,里面的元素的變更將導(dǎo)致原變量的值隨之改變

        拷貝對(duì)象,代碼如下:

        const time = {    year: 2021,    month: 7,    day: {        value: 1,    },};const copyTime = { ...time };console.log(copyTime); // { year: 2021, month: 7, day: { value: 1 } }

        擴(kuò)展運(yùn)算符拷貝對(duì)象只會(huì)在一層進(jìn)行深拷貝,從下面代碼是基于上面代碼:

        copyTime.day.value = 2;copyTime.month = 6;console.log(copyTime); // { year: 2021, month: 6, day: { value: 2 } }console.log(time); // { year: 2021, month: 7, day: { value: 2 } }

        從打印的結(jié)果看,擴(kuò)展運(yùn)算符只對(duì)對(duì)象第一層進(jìn)行了深拷貝。

        嚴(yán)格來(lái)講,擴(kuò)展運(yùn)算符不執(zhí)行深拷貝

        2、合并操作

        先來(lái)看數(shù)組的合并,如下:

        const halfMonths1 = [1, 2, 3, 4, 5, 6];const halfMonths2 = [7, 8, 9, 10, 11, 12];
        const allMonths = [...halfMonths1, ...halfMonths2];console.log(allMonths); // [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ]

        合并對(duì)象,在合并對(duì)象時(shí),如果一個(gè)鍵已經(jīng)存在,它會(huì)被具有相同鍵的最后一個(gè)對(duì)象給替換。

        const time1 = {    month: 7,    day: {        value: 1,    },};const time2 = {    year: 2021,    month: 8,    day: {        value: 10,    },};const time = { ...time1, ...time2 };console.log(time); // { month: 8, day: { value: 10 }, year: 2021 }

        3、參數(shù)傳遞

        const sum = (num1, num2) => num1 + num2;
        console.log(sum(...[6, 7])); // 13console.log(sum(...[6, 7, 8])); // 13

        從上面的代碼看,函數(shù)定義了多少個(gè)參數(shù),擴(kuò)展運(yùn)算符傳入的值就是多少個(gè)。

        和 math 函數(shù)一起使用,如下:

        const arrayNumbers = [1, 5, 9, 3, 5, 7, 10];const min = Math.min(...arrayNumbers);const max = Math.max(...arrayNumbers);console.log(min); // 1console.log(max); // 10

        4、數(shù)組去重

        與 Set 一起使用消除數(shù)組的重復(fù)項(xiàng),如下:

        const arrayNumbers = [1, 5, 9, 3, 5, 7, 10, 4, 5, 2, 5];const newNumbers = [...new Set(arrayNumbers)];console.log(newNumbers); // [ 1,  5, 9, 3, 7, 10, 4, 2 ]

        5、字符串轉(zhuǎn)字符數(shù)組

        String 也是一個(gè)可迭代對(duì)象,所以也可以使用擴(kuò)展運(yùn)算符 ... 將其轉(zhuǎn)為字符數(shù)組,如下:

        const title = "china";const charts = [...title];console.log(charts); // [ 'c', 'h', 'i', 'n', 'a' ]

        進(jìn)而可以簡(jiǎn)單進(jìn)行字符串截取,如下:

        const title = "china";const short = [...title];short.length = 2;console.log(short.join("")); // ch

        6、NodeList 轉(zhuǎn)數(shù)組

        NodeList 對(duì)象是節(jié)點(diǎn)的集合,通常是由屬性,如 Node.childNodes 和方法,如 document.querySelectorAll 返回的。

        NodeList 類(lèi)似于數(shù)組,但不是數(shù)組,沒(méi)有 Array 的所有方法,例如find、map、filter 等,但是可以使用 forEach() 來(lái)迭代。

        可以通過(guò)擴(kuò)展運(yùn)算符將其轉(zhuǎn)為數(shù)組,如下:

        const nodeList = document.querySelectorAll(".row");const nodeArray = [...nodeList];console.log(nodeList);console.log(nodeArray);

        7、解構(gòu)變量

        解構(gòu)數(shù)組,如下:

        const [currentMonth, ...others] = [7, 8, 9, 10, 11, 12];console.log(currentMonth); // 7console.log(others); // [ 8, 9, 10, 11, 12 ]

        解構(gòu)對(duì)象,如下:

        const userInfo = { name: "Crayon", province: "Guangdong", city: "Shenzhen" };const { name, ...location } = userInfo;console.log(name); // Crayonconsole.log(location); // { province: 'Guangdong', city: 'Shenzhen' }

        8、打印日志

        在打印可迭代對(duì)象的時(shí)候,需要打印每一項(xiàng)可以使用擴(kuò)展符,如下:

        const years = [2018, 2019, 2020, 2021];console.log(...years); // 2018 2019 2020 2021

        總結(jié)

        擴(kuò)展運(yùn)算符 … 讓代碼變得簡(jiǎn)潔,應(yīng)該是ES6中比較受歡迎的操作符了。


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

        請(qǐng)點(diǎn)擊下方公眾號(hào)


        瀏覽 73
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評(píng)論
        圖片
        表情
        推薦
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            9l视频自拍九色9l视频成人 | 一区二区三区三级片 | 免费看AA片 | 欧美成人免费观看 | 靠逼视屏免费 | 男人透女人逼视频 | 总裁双性受在会议室被h | 免费在线无码视频 | 久久偷拍无码 | 天堂视频中文在线观看 |