es6結構賦值(3篇)


es6結構賦值:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>es6結構賦值</title>
</head>
<body>

    <script>
        //es6結構賦值
        let arr=["held","word","good"];
        let [firstname,twoname] =  arr
        console.log(firstname,twoname); //held word


        let demo = ["a","b","c","d"];
        let [name1, , name3] = demo;
        console.log(name1,name3);//a c


        //屬性賦值
        let user = {name:"張三",old:"18"};
        [user.name,user.old] = [1,2]
        console.log(user);//{name: 1, old: 2}

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

 數組結構賦值

<script>
        //數組結構賦值
        let arr = [ 1,2,3,4,5,6,7,8,9]
        let [firstname,curname,...last] = arr
        console.log(firstname,curname,last);//1 2 (7) [3, 4, 5, 6, 7, 8, 9]
    </script>

 對象結構賦值

<script>
        //object結構賦值
        let options = {
            title:"menu",
            width:100,
            heigth:100
        }
        //左邊是變量右邊是賦值
        let {title,width,heigth} = options;
        console.log(title,width,heigth)//menu 100 100
    </script>

 對象結構賦值2

<script>
        //object結構賦值
        let options = {
            title:"menu",
            heigth:100
        }
        //默認值
        let {title:title2,width=130,heigth} = options;
        console.log(title2,width,heigth)//menu 130 100
    </script>

對象結構賦值 3

<script>
        //object結構賦值
        let options = {
            title:"menu",
            width:100,
            heigth:100
        }
        //...last
        let {title,...last} = options;
        console.log(title,last)//menu {width: 100, heigth: 100}
    </script>

對象結構嵌套賦值

<script>
        //object結構嵌套賦值
        let options = {
            size:{
                width:100,
                heigth:200
            },
            items:['cake','donut'],
            heigth:100
        }
        let {size:{width}} = options;
        console.log(width)//100
    </script>

 對象結構嵌套賦值2

<script>
        //object結構嵌套賦值
        let options = {
            size:{
                width:100,
                heigth:200
            },
            items:['cake','donut'],
            heigth:100
        }
        let {size:{width:kuan,heigth},items:[item1]} = options;
        console.log(kuan,heigth,item1)//100 200 "cake"
    </script>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM