es6 语法 (set 和 map)

2018-06-24 00:35:59来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

{
    let list = new Set();
    list.add(5);
    list.add(7);

    console.log('size', list, list.size); //{5, 7} 2
}

{
    let arr = [1, 2, 3, 4, 5];
    let list = new Set(arr);

    console.log('size', list.size); //5
}

{
    let list = new Set();
    list.add(1);
    list.add(2);
    list.add(1);

    console.log('list', list); //{1,2}

    let arr = [1, 2, 3, 1, '2'];
    let list2 = new Set(arr);

    console.log('unique', list2); //{1,2,3,'2'}
}

{
    let arr = ['add', 'delete', 'clear', 'has'];
    let list = new Set(arr);

    console.log('has', list.has('add')); //true
    console.log('delete', list.delete('add'), list); //{"delete", "clear", "has"}
    list.clear(); //清空
    console.log('list', list); //{}
}

{
    let arr = ['add', 'delete', 'clear', 'has'];
    let list = new Set(arr);
    console.log('list', list); //{"add":"add","delete":"delete","clear":"clear","has":"has"}
    for (let key of list.keys()) {
        console.log('keys', key); //add delete clear has
    }
    for (let value of list.values()) {
        console.log('value', value); //add delete clear has
    }
    for (let [key, value] of list.entries()) {
        console.log('entries', key, value); // add add;delete delete;clear clear;has has;
    }

    list.forEach(function(item) { console.log('item', item); }) //add delete clear has
}


{
    let weakList = new WeakSet();

    let arg = {};

    weakList.add(arg);

    // weakList.add(2);

    console.log('weakList', weakList); //{}
}

{
    let map = new Map();
    console.log('mapfirst', map); //{}
    let arr = ['123'];

    map.set(arr, 456);
    console.log('maptwo', map);//{array(1)=>456}
    console.log('map', map, map.get(arr)); //456
}

{
    let map = new Map([
        ['a', 123],
        ['b', 456]
    ]);
    console.log('map args', map);//{"a"=>123,"b"=>456}
    console.log('size', map.size);//2
    console.log('delete', map.delete('a'), map);//true , {"b"=>456}
    console.log('clear', map.clear(), map);//{}
}

{
    let weakmap = new WeakMap();

    let o = {};
    weakmap.set(o, 123);
    console.log(weakmap.get(o));//123
}

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:twbsPagination.js分页插件使用心得

下一篇:js基础