首页 > 其他 > 详细

[ES6] Set && WeakSet

时间:2016-01-14 06:16:45      阅读:175      评论:0      收藏:0      [点我收藏+]

 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);

 

[ES6] Set && WeakSet

原文:http://www.cnblogs.com/Answer1215/p/5129043.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!