<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12
      Fork me on GitHub

      JavaScript 解構(Destructuring)簡介

      解構是 ES6 引入的一種語法,它允許從數組或對象中快速提取值,并將這些值賦給變量。解構使代碼更加簡潔、可讀性更強,特別是在處理復雜的數據結構時。


      解構賦值分類

      1. 數組解構賦值
      2. 對象解構賦值
      3. 嵌套解構
      4. 默認值
      5. 剩余(Rest)解構

      1. 數組解構賦值

      基礎用法

      從數組中按順序提取值,賦給變量:

      const arr = [1, 2, 3];
      const [a, b, c] = arr;
      
      console.log(a); // 1
      console.log(b); // 2
      console.log(c); // 3
      

      跳過元素

      可以通過省略的位置跳過某些元素:

      const arr = [1, 2, 3, 4];
      const [a, , c] = arr; // 跳過第二個元素
      
      console.log(a); // 1
      console.log(c); // 3
      

      結合剩余操作符

      將數組的剩余部分賦值給變量:

      const arr = [1, 2, 3, 4];
      const [a, ...rest] = arr;
      
      console.log(a);    // 1
      console.log(rest); // [2, 3, 4]
      

      2. 對象解構賦值

      基礎用法

      從對象中提取值,變量名需與對象的鍵名一致:

      const obj = { name: "John", age: 30 };
      const { name, age } = obj;
      
      console.log(name); // "John"
      console.log(age);  // 30
      

      修改變量名

      通過 : 指定變量名:

      const obj = { name: "John", age: 30 };
      const { name: userName, age: userAge } = obj;
      
      console.log(userName); // "John"
      console.log(userAge);  // 30
      

      結合默認值

      在解構時為變量設置默認值:

      const obj = { name: "John" };
      const { name, age = 25 } = obj;
      
      console.log(name); // "John"
      console.log(age);  // 25 (age 默認值)
      

      提取嵌套對象

      可以解構嵌套對象:

      const obj = { user: { name: "John", age: 30 } };
      const { user: { name, age } } = obj;
      
      console.log(name); // "John"
      console.log(age);  // 30
      

      3. 嵌套解構

      嵌套數組

      數組中嵌套數組的解構:

      const arr = [1, [2, 3]];
      const [a, [b, c]] = arr;
      
      console.log(a); // 1
      console.log(b); // 2
      console.log(c); // 3
      

      嵌套對象

      對象中嵌套對象的解構:

      const obj = { a: { b: { c: 10 } } };
      const { a: { b: { c } } } = obj;
      
      console.log(c); // 10
      

      4. 默認值

      數組默認值

      解構時如果值為 undefined,會使用默認值:

      const arr = [1];
      const [a, b = 2] = arr;
      
      console.log(a); // 1
      console.log(b); // 2
      

      對象默認值

      同樣適用于對象解構:

      const obj = { name: "John" };
      const { name, age = 30 } = obj;
      
      console.log(name); // "John"
      console.log(age);  // 30
      

      5. 剩余(Rest)解構

      數組中的剩余

      可以通過 ... 操作符收集數組中的剩余元素:

      const arr = [1, 2, 3, 4];
      const [a, ...rest] = arr;
      
      console.log(a);    // 1
      console.log(rest); // [2, 3, 4]
      

      對象中的剩余

      收集對象中未解構的屬性:

      const obj = { name: "John", age: 30, gender: "male" };
      const { name, ...rest } = obj;
      
      console.log(name); // "John"
      console.log(rest); // { age: 30, gender: "male" }
      

      6. 結合函數參數解構

      數組解構參數

      函數參數直接解構:

      function sum([a, b]) {
        return a + b;
      }
      
      console.log(sum([1, 2])); // 3
      

      對象解構參數

      通過對象解構指定參數:

      function greet({ name, age }) {
        return `Hello, ${name}. You are ${age} years old.`;
      }
      
      console.log(greet({ name: "John", age: 30 })); 
      // "Hello, John. You are 30 years old."
      

      7. 解構的常見應用場景

      交換變量值

      let a = 1, b = 2;
      [a, b] = [b, a];
      
      console.log(a); // 2
      console.log(b); // 1
      

      從函數返回值中提取

      function getCoordinates() {
        return [10, 20];
      }
      
      const [x, y] = getCoordinates();
      console.log(x, y); // 10, 20
      

      從對象中提取部分值

      const user = { id: 1, name: "John", age: 30 };
      const { id, name } = user;
      
      console.log(id);   // 1
      console.log(name); // "John"
      

      設置默認配置

      const config = { width: 800 };
      const { width, height = 600 } = config;
      
      console.log(width);  // 800
      console.log(height); // 600
      

      總結

      解構賦值是 JavaScript 中非常強大的語法工具,可以大幅簡化代碼,特別是在處理復雜數據結構、函數參數和默認值時。掌握數組和對象解構是寫高效 JavaScript 的重要技能!

      posted @ 2024-12-24 13:58  極度恐慌_JG  閱讀(120)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 中文字幕av无码免费一区| 国产成人精品一区二区秒拍1o | 影音先锋大黄瓜视频| 婷婷综合亚洲| 亚洲国产午夜精品理论片在线播放| 最近中文字幕国产精品| 日韩精品国内国产一区二| 亚洲国产午夜精品福利| 精品一区二区三区无码视频| 成人激情视频一区二区三区| 国产中文字幕精品视频| 麻豆国产成人av高清在线| 深夜精品免费在线观看| 久久国产精品久久精品国产| 另类 专区 欧美 制服| 人妻中文字幕精品一页| 乌苏市| 免费网站看sm调教视频| 亚洲精品中文字幕尤物综合 | 南阳市| 久久国产精品99久久蜜臀| 亚洲精品中文av在线| 寻甸| 色色97| 亚洲高清 一区二区三区| 97久久综合亚洲色hezyo| 欧美成人精品三级网站视频| 国产网友愉拍精品视频手机| 久久免费看少妇免费观看| 日韩一区在线中文字幕| 蜜臀午夜一区二区在线播放| 国产精品一久久香蕉产线看| 久久精品国产www456c0m| 婷婷丁香五月亚洲中文字幕| 一区二区三区激情都市| 国产成人精品午夜福利| 国产精品国产精品国产精品| 国产美女69视频免费观看| 四虎成人精品永久免费av| 男人天堂亚洲天堂女人天堂| 欧洲免费一区二区三区视频|