1. 程式人生 > >ES6入門到進階 第二節 解構賦值

ES6入門到進階 第二節 解構賦值

<!DOCTYPE html>
<html>
<head>
    <title>解構賦值</title>
</head>
<body>
    <script type="text/javascript">
        /*
        解構賦值 let [a, b, c] = [12, 5, 6]
        左右兩邊,結構格式保持一致
         */
        
        let [a, b, c] = [12, 5, 6];
        console.log(a, b, c);

        let json = {
            name: 'llh',
            age: 18,
            job: "it"
        };
        // let {name, age, job} = json;
        // console.log(name, age, job);
        //起別名 用冒號
        let {name, age, job:k} = json;
        console.log(name, age, k);
        //給預設值
        let [d, e, f = "暫無資料"] = ["aa", "bb"];
        console.log(d, e , f);

        //交換兩個變數的值
        let g = 12;
        let h = 5;
        [g, h] = [h, g];
        console.log(g, h);

    </script>
</body>
</html>