zoukankan      html  css  js  c++  java
  • [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);
  • 相关阅读:
    UML序列图
    接口初探
    Discuz初探
    Vim指令学习
    UCenter Home代码研读之space.php
    建站须知
    linux指令之文件的创建、查询、修改
    InitPHP初探
    php环境搭建
    Zend Framework学习之Zend_Db 数据库操作
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5129043.html
Copyright © 2011-2022 走看看