粗看ES6之解構賦值

標籤: javascript es6javascript


什麼是解構賦值?

示例以下:html

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>解構賦值</title>
</head>
<body>
  <script>
    let [a,b] = [1,2];
    let {c,d} = {c:11,d:22};
    console.log(a,b,c,d);
  </script>
</body>
</html>

測試地址java

注意事項

  • 左右二邊結構要相同,指[]=[]/{}={}
  • 聲明和賦值必須在一句話裏完成
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>解構賦值規則</title>
</head>
<body>
  <script>
    
    //左右二邊結構必須相同
    //let [a,b] = {a:1,b:2}; //不成功
    //let {a,b} = [1,2]; //不成功
    //console.log(a,b)
    //聲明和賦值必須在同一個語句裏完成
    /*
    let [a,b];
    [a,b] = [1,2];//不成功
    console.log(a,b)
    */
    
    let [a,b] = [1,2];//成功
    let [{c,d},[e,f],g,h,i]=[{c:11,d:22},[111,222],333,[1,2],{a:1}]//成功
    console.log(a,b,c,d,e,f,g,h,i);
  </script>
</body>
</html>

測試地址es6

相關文章
相關標籤/搜索