JavaScript, Set and Map

https://www.boatsky.com/blog/47

 

 

  The partial data type ES5 original type and the object type. Where the original types Number, Boolean, String, null, undefined, ES6 increase Symbol. Object object type ES5 is, from the functional distinction, subdivided into several broad Object "sub-object", Common Object ( "designated value" unordered collection), (ordered set of values ​​numbered) arrays, functions, (objects having executable code associated with it), for ES6 added set (set of non-repeating elements), (a collection of multiple sets of key-value pairs) Map.
   
    & Lt; a href = & quot; http: //www.boatsky.com/blog/47.html" & gt; Set with Map & lt; / a & gt; early analog implementation, there are prior ES6, ES6 make this standard to use easier.
   
  ###Set
    Set is a collection of elements without duplication, because the key value is the same, so we only discuss its value.
   
    Its value may be the value of all the raw data types Number, Boolean, String, null, undefined, Symbol, and object data type Object, Array, Function, Set, Map and so on, and it will not be typecast before and after access.
   
    Set offers add, has, size, delete method of operation
  #####add(value)
  Added value, return target set after the increase.
  #####has(value)
  Determine whether the value is, returns a Boolean value
  #####size
  Determining length, returns the number
  #####delete(value)
  Delete the value and returns a Boolean value
  #####clear()
  Empty collection
   
  demo:
  ```javascript
  console.log(set.add('5'));//set对象
  console.log(set.has(5));//false
  console.log(set.has('5'));//true
  console.log(set.size);//1
  console.log(set.delete('5'));//true
  console.log(set.has('5'));//false
  ```
    Set可使用keys,values,entries,forEach方法。因为key与value是一样的,keys与values结果都一样。entries返回item的是key与value数组。forEach为了与其他对象参数保持一致,第一个参数与第二个参数很多余的一样了。
  demo:
  ```javascript
  let set = new Set();
  let aObj = {a: 1};
  let bObj = {b: 2};
  set.add(aObj).add(bObj);
  for (let k of set.keys()) {
  console.log(k);
  }
  //{a: 1}
  //{b: 2}
   
  for (let v of set.values()) {
  console.log(v);
  }
  //{a: 1}
  //{b: 2}
   
  for (let item of set.entries()) {
  console.log(item);
  }
  // [{a: 1},{b: 2}]
  // [{a: 1},{b: 2}]
   
  set.forEach(function(key, value, ownSet) {
  console.log(valueCopy);
  });
  //{a: 1}
  //{b: 2}
  ```
   
  因为Set是不可重复的,所以数组的去重一行简单的代码便实现了:
  ```javascript
  let arr = [1, 1, 2, 2, 3, 3];
  [...new Set(arr)];
  ```
   
  Set是强引用的集合,如果原始引用被移除了,Set集合并不会删除原值,垃圾回收机制也无法对其回收:
  ```javascript
  let set = new Set();
  let key = {};
  set.add(key);
  console.log(set.has(key));//true
  for (let item of set.entries()) {
  console.log(item);
  }
  // [{},{}]
   
  key = null;
  console.log(set.has(key));//false
  for (let item of set.entries()) {
  console.log(item);
  }
  // [{},{}]
  ```
   
  内存无法释放,可能引起内存泄漏,为此Set有个WeakSet扩展,使用弱引用,解决这个问题,但WeakSet只支持add,has,delete方法,其他方法都不支持,并且不能传入原始值。虽然可以用上述的has判断,但我们需传递强引用才能验证弱引用是存被移除,这无法在代码上实现,因为我们只能传递弱引用key:
  ```javascript
  let set = new WeakSet();
  let key = {};
  set.add(key);
  console.log(set.has(key));//true
  key = null;
  console.log(set.has(key));//false
  ```
  此时我们无从得知,第二个set.has(key)为false是因为
  [{},{}] !== [null,null]还是set是空集合。只能从概念上得知,是后者。
   
  ###Map
    Map是多组键值对的有序集合,与Set几乎是一样的,参考上述Set即可,在此不做复述,只简述不同之处。
   
    key与value可以不同。Map与Set相比,新增了set,get方法。
   
  #####set(key,value)
  设置值,返回map
  #####get(key)
  取值
   
    Map也有一个与之对应的WeakMap,参见上述WeakSet,不同之处,key必须为对象,且不能为null,否则报错,而value则无限制。</textarea>
发布了349 篇原创文章 · 获赞 493 · 访问量 193万+

Guess you like

Origin blog.csdn.net/starzhou/article/details/104859808