zoukankan      html  css  js  c++  java
  • 十.数组解构

    const numbers = ['one', 'two', 'three', 'four']
    

      

    es5:

    const one = numbers[0];    //one     
    const two = numbers[1];    //two
    

      

    es6: 获取到相应位置的数组原素的值

    const [one, two] = numbers;

    console.log(one,two);  //one two

    //如果要获取数组0跟3 位置的元素的话,把那个位置留出来就行
    const [one, , tow] = numbers;

    //如果你想获取第一个元素的值和后面所有元素的值的话 (...others必须是在最后的一个位置)
    const [one,...others] = numbers;
    console.log(one,others);  // one ["two","three","four"]

      

    es6默认参数:

    const details = ['JellyBool', 'wangrong.com', null];
    
    const [name,website,category= 'PhP'] = details;
    
    console.log(name, website, category);    // JellyBool wangrong.com null (只有category为undefined时,category值才为Php)
    

      

    例子:交换 a 跟 b 的值

    let a = 10;
    let b = 20;

    es5:

    let temp;
    temp = a;
    a = b;
    b = temp;
    console.log(a,b);    //20 10
    

      

    es6:

    [a,b] = [b,a];
    
    console.log(a,b);    //20 10
    

      

  • 相关阅读:
    【笔记】隐式寻址方式(pending...)
    【笔记】有结构文件(pending...)
    【笔记】目录项(pending...)
    【笔记】主存储器
    Ubuntu 16.04 升级OpenSSH至7.7p1
    awk用法笔记
    find命令笔记
    VIM的使用
    Shell
    Linux文本处理三剑客
  • 原文地址:https://www.cnblogs.com/wangRong-smile/p/11926453.html
Copyright © 2011-2022 走看看