简体   繁体   English

ES6 Set 和 WeakSet 有什么区别?

[英]What's the difference between ES6 Set and WeakSet?

ECMAScript 6 has these very similar collections: Set and WeakSet . ECMAScript 6 有这些非常相似的集合: SetWeakSet What is the difference between them?它们之间有什么区别?

The main difference is that references to objects in Set are strong while references to objects in WeakSet are weak. 主要区别在于对Set中对象的引用很强,而对WeakSet中对象的引用很弱。 This means that an object in WeakSet can be garbage collected if there is no other reference to it . 这意味着如果没有其他引用,WeakSet中的对象可以被垃圾收集

Other differences (or rather side-effects) are: 其他差异(或相反的副作用)是:

  • Sets can store any value. 集可以存储任何值。 WeakSets are collections of objects only. WeakSets只是对象的集合。
  • WeakSet does not have size property. WeakSet没有size属性。
  • WeakSet does not have clear, keys, values, entries, forEach methods. WeakSet没有clear,keys,values,entries,forEach方法。
  • WeakSet is not iterable. WeakSet不可迭代。

Summary: 摘要:

Weaksets are javascript objects which holds a collection of objects. 弱集是javascript对象,它包含一组对象。 Due to the nature of a set only one object reference of the same object may occur within the set. 由于集合的性质,集合中只能出现一个对象引用相同的对象。 A Weakset differs from a normal set in the following ways: Weakset在以下方面与普通集不同:

  1. Weaksets can only hold objects within its collection, no primitive values (eg int , boolean , string ) are allowed. 弱集只能在其集合中保存对象,不允许使用原始值(例如intbooleanstring )。
  2. References to the objects are held weak . 对象的引用很弱 This means that whenever there is no other reference towards the object besides the WeakSet , the object can be be garbage collected (ie the JS engine will free the memory which object the reference was pointing to). 这意味着除了WeakSet之外没有其他对象的WeakSet ,对象可以被垃圾收集(即JS引擎将释放引用所指向的对象的内存)。

Example: 例:

 let myWeakSet = new WeakSet(); let obj = {}; myWeakSet.add(obj); console.log(myWeakSet.has(obj)); // break the last reference to the object we created earlier obj = 5; // false because no other references to the object which the weakset points to // because weakset was the only object holding a reference it released it and got garbage collected console.log(myWeakSet.has(obj)); 

Set:- A Set is a collection of values, where each value may occur only once. 设置: - Set是值的集合,其中每个值只能出现一次。 And main method are add, delete, has, clear and size. 主要方法有add,delete,has,clear和size。

WeakSet:- WeakSet objects allows you to store collection of unique key.“ WeakSet ” keys cannot be primitive types. WeakSet: - WeakSet对象允许您存储唯一键的集合。“ WeakSet ”键不能是原始类型。 Nor they can be created by an array or another set. 它们也不能由数组或其他集创建。 Values of WeakSet must be object reference. WeakSet的值必须是对象引用。

  • Sets allows to store only once. 集只允许存储一次。
  • The elements stored in set does not have a key or index. 存储在集合中的元素没有键或索引。 So it is difficult to retrieve an element using default method like get() 因此很难使用默认方法检索元素,如get()
  • A WeakSet only accepts objects as its values. WeakSet仅接受对象作为其值。
  • A weakset doesnot prevent garbage collection if there aren't any other references to an object stored (the reference is weak) 如果没有对存储的对象的任何其他引用(引用很弱),则weakset不会阻止垃圾回收

这是用例的解释,有助于更好地理解这个概念

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM