美文网首页
ES6变量解构的用途

ES6变量解构的用途

作者: me_coder | 来源:发表于2019-12-02 19:10 被阅读0次

    1、交换变量的值

    let x = 1;
    let y = 2;
    
    [x, y] = [y, x];
    

    2、从函数返回多个值

    function example() {
      return [1, 2, 3];
    }
    let [a, b, c] = example();
    
    // 返回一个对象
    
    function example() {
      return {
        foo: 1,
        bar: 2
      };
    }
    let { foo, bar } = example();
    

    3、函数参数的定义

    // 参数是一组有次序的值
    function f([x, y, z]) { ... }
    f([1, 2, 3]);
    
    // 参数是一组无次序的值
    function f({x, y, z}) { ... }
    f({z: 3, y: 2, x: 1});
    

    4、提取json数据

    let jsonData = {
      id: 42,
      status: "OK",
      data: [867, 5309]
    };
    
    let { id, status, data: number } = jsonData;
    
    console.log(id, status, number);
    // 42, "OK", [867, 5309]
    

    5、函数参数的默认值

    jQuery.ajax = function (url, {
      async = true,
      beforeSend = function () {},
      cache = true,
      complete = function () {},
      crossDomain = false,
      global = true,
      // ... more config
    } = {}) {
      // ... do stuff
    };
    

    6、遍历Map结构

    const map = new Map();
    map.set('first', 'hello');
    map.set('second', 'world');
    
    for (let [key, value] of map) {
      console.log(key + " is " + value);
    }
    // first is hello
    // second is world
    

    如果只想获取键名,或者只想获取键值,可以写成下面这样:

    // 获取键名
    for (let [key] of map) {
      // ...
    }
    
    // 获取键值
    for (let [,value] of map) {
      // ...
    }
    

    7、输入模块的指定方法

    const { SourceMapConsumer, SourceNode } = require("source-map");
    

    相关文章

      网友评论

          本文标题:ES6变量解构的用途

          本文链接:https://www.haomeiwen.com/subject/izifgctx.html