• [ES6] Set && WeakSet


     Limitations With Array

    Arrays don't enforce uniqueness of items. Diplicate entries are allowed.

    Using Sets 

    let tags = new Set() ;
    tags.add('Javascript');
    tags.add('Programming');
    tags.add('Web');
    
    console.log(`Total items ${tags.size}`);

    Sets and for...of

    let tags = new Set();
    
    tags.add("JavaScript");
    tags.add("Programming");
    tags.add("Web");
    
    for( let tag of tags ){
      console.log(`Tag: ${tag}`);
    }

    Sets and Destructuring 

    let tags = new Set();
    
    tags.add("JavaScript");
    tags.add("Programming");
    tags.add("Web");
    
    let [first] = tags;
    
    console.log( `First tag: ${first}` );

    WeakSets

    The WeakSet is a more memory efficient type of Set where only objets are allowed to be stored.

    WeakSets in Action 

    let allPosts = new WeakSet();
    
    let post1 = { title: "ES2015" };
    let post2 = { title: "CoffeeScript" };
    let post3 = { title: "TypeScript" };
    
    allPosts.add( post1 );
    allPosts.add( post2 );
    allPosts.add( post3 );

    How can we query the allPosts WeakSet to determine whether it has the post2 object?

    allPosts.has(post2);
  • 相关阅读:
    课程个人总结
    构建之法阅读笔记06
    构建之法读后感5
    第五周进度条
    提高自身能力
    活动图与状态机图
    对分析业务模型----类图的学习与认识
    需求分析工作的基本道理
    问题账户需求分析
    2016秋季个人阅读计划
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5129043.html
Copyright © 2020-2023  润新知