时间: 2020-11-25|45次围观|0 条评论

变量的结构赋值。
基本概念:
  本质上就是一中匹配模式,只要等号两边的模式相同,那么左边的变量就可以被赋予对应的值:
  1、数组的结构赋值。
  2、对象的结构赋值。
  3、基本类型的结构赋值。

    let [a,b,c] = [1,2,3];
    console.log(a,b,c); ///输出 1,2,3

 

  1、数组的结构赋值。

    let [a, [ [b] , c] ] = [ 1,[ [ 2 ] , 3 ] ];
    console.log(a,b,c); ///输出 1,2,3

    let [ , , c]=[ 1,2,3]
    console.log(c) ///输出3

    let [x] = [];
    console.log (x) ///输出 undefined ;

    let [ y =1 ] = [];

    console.log(y) ///输出 1

 

  2、对象的结构赋值。

    let {a,b} = {b:"bbb",a:"aaa"}
    console.log(a,b) ///输出 aaa , bbb

    let {a:b} = {a:1}
    console.log(b) //输出 1
    console.log(a) // 错误信息

 

  3、基本类型的结构赋值。

    let [a,b,c]="1234";
    console.log(a,b,c,d); //输出 1234

    let {length:len} = "miaov";
    console.log(len) ///输出 5

    let {tostring : ts}=1;
    let {toString: bs}=true;

    console.log(ts===Number.prototype.toString); //输出 true
    console.log(bs===Boolean.prototype.toString); //输出 true

    //null 和 undefined 不能进行结构赋值。
    //let[a] = null

 

 

以上。

转载于:https://www.cnblogs.com/zyhbook/p/9425577.html

原文链接:https://blog.csdn.net/weixin_30342827/article/details/96355535

本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。

本博客所有文章如无特别注明均为原创。
复制或转载请以超链接形式注明转自起风了,原文地址《ES6—— 变量的结构赋值
   

还没有人抢沙发呢~