解构赋值、

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    
</body>
<script>
    // 新增赋值方式:解构赋值
    // 解构:解开,结构

    // 数组的解构:按照顺序一一对应,没有对应,为undefined
     var arr = ["hello",3.1415,true];
     var [c,b,a,d,e,f] = arr;
     console.log(a);//true
     console.log(b);//undefined
     console.log(c);//hello
     console.log(d);//u
     console.log(e);//u
     console.log(f);//u

//     字符的解构:按照顺序一一对应,没有对应,为undefined
     var str = "hi";
     var [a,b,c,d] = str;
     console.log(a)//h
     console.log(b)//i
     console.log(c)//u
     console.log(d)//u

    // 对象的解构:按照key一一对应,没有对应,为undefined
     var obj = {
         name:"admin",
         age:18,
         sex:1
     }
     var {sex,age,name,like} = obj;
     console.log(name)//admin
     console.log(age)//18
     console.log(sex)//1
     console.log(like)//undefined


    // 参数的解构
     function fn({name,age}){
         // name
         // age
     }
     fn({name:"admin",age:19})

     function fn(){
         return ["hello","world"]
     }
     var [a,b] = fn();

     var a = 10;
     var b = 20;
     var [b,a] = [a,b];
     console.log(a,b)//20 10
</script>
</html>

 

posted @ 2019-08-30 16:44  菜鸟小何  阅读(281)  评论(0编辑  收藏  举报