• es6基础(2)--解构赋值


     1 //解构赋值
     2 
     3 {
     4     let a,b,rest;
     5     [a,b]=[1,2];
     6     console.log(a,b);
     7 }
     8 //数组解构赋值
     9 {
    10     let a,b,rest;
    11     [a,b,...rest]=[1,2,3,4,5,6];
    12     console.log(a,b,rest);
    13 }
    14 //对象解构赋值
    15 {
    16     let a,b;
    17     ({a,b}={a:1,b:2})
    18     console.log(a,b);
    19 }
    20 
    21 {
    22     let a,b,c,rest;
    23     [a,b,c=3]=[1,2];
    24     console.log(a,b,c);
    25 }
    26 //变量交换
    27 {
    28     let a=1;
    29     let b=2;
    30     [a,b]=[b,a];
    31     console.log(a,b);
    32 }
    33 
    34 {
    35     function f(){
    36         return [1,2]
    37     }
    38     let a,b;
    39     [a,b]=f();
    40     console.log(a,b)
    41 }
    42 {
    43     function f(){
    44         return [1,2,3,4,5,6];
    45     }
    46     let a,b,c;
    47     [a,,,b]=f();
    48     console.log(a,b);
    49 }
    50 //当只要数组内的某几个的时候
    51 {
    52     function f(){
    53         return [1,2,3,4,5,6];
    54     }
    55     let a,b,c;
    56     [a,...b]=f();
    57     console.log(a,b);
    58 }
    59 //对象的赋值
    60 {
    61     let o={p:42,q:true};
    62     let {p,q}=o;
    63     console.log(p,q);
    64 }
    65 {
    66     let {a=10,b=5}={a:3};
    67     console.log(a,b);
    68 }
    69 {
    70     let metaData={
    71         title:'abc',
    72         test:[{
    73             title:'test',
    74             desc:'description'
    75         }]
    76     }
    77     let {title:esTitle,test:[{title:cntitle}]}=metaData;
    78     console.log(esTitle,cntitle);
    79 }
  • 相关阅读:
    XJTUOJ13 (数论+FFT)
    ZOJ3956 ZJU2017校赛(dp)
    ZOJ3953 ZJU2017校赛(贪心)
    2014ACM/ICPC亚洲区西安站现场赛 F color(二项式反演)
    LOJ10129
    loj 10127最大数
    noip 选择客栈
    LOJ10121 与众不同
    CQOI 2006 简单题
    校门外的树
  • 原文地址:https://www.cnblogs.com/chenlw/p/9208572.html
Copyright © 2020-2023  润新知