《ES6标准入门(第3版)》学习笔记10:chapter_3-7 解构赋值的用途

这是第10篇笔记!
让学习“上瘾”,成为更好的自己!!!

<!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>用途</title>
</head>
<body>
    <script>
    // 1, 交换变量的值 
    let x = 1;
    let y = 2;
    [x, y] = [y, x];
    console.log(x);
    console.log(y);



    // 2, 从函数返回多个值
    // 返回一个数组
        // function example1(){
        //     return [1, 2, 3];
        // }
        // let [a, b, c] = example1();


    // 返回一个对象 
    function example2(){
        return {
            foo: 1,
            bar: 2
        };
    }
    let {foo, bar} = example2();





    // 3, 函数参数的定义 --> 解构赋值可以方便地将一组参数与变量名对应起来
    // 参数是一组有次序的值
    function f1([x, y, z]){
        return x+y+z;
    }
    console.log(f1([1, 2, 3]));

    // 参数是一组无次序的值
    function f2({x, y, z}){
        return x+y+z;
    }
    console.log(f2({x: 2, y: 23, z: 1}));


    // 4, 提取JSON数据
    let jsonData = {  // 注意,这不是Json数据格式的书法!
        id: 42,
        status: "ok",
        data: [898, 787]
    };

    let {id, status, data} = jsonData;
    console.log(id, status, data);  // 42 ,"ok" ,[898, 787]

    // 5, 函数参数的默认值  --> 避免在函数体内再写“var foo = config.foo || 'default foo' ”
    // jQuery.ajax = function(url, {
    //    async = true,
    //    beforeSend = function(){},
    //    cache = true,
    //    complete = function(){},
    //    crossDomain = false,
    //    global = true,
    //     // ...
    // }){
    //     // do stuff
    // };


    // 6, 遍历Map结构
    // 任何部署了Iterator结构的对象都可以用“for...of”循环遍历
    // Map结构原生支持Iterator接口,配合变量的结构赋值获取键名和键值很方便

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

    }




    // 7, 输入模块的指定方法 
    // 加载模块时候,往往需要制定输入的方法
    // const {SourceMapConsumer, SourceNode} = require('source-map');
    
    
    
    
    
    </script>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值