ES6: Set 与 Map
Set
- let my_set = new Set();
- let my_set = new Set([1, 1, 2, 2]);
- my_set.add(5);
- my_set.delete(5);
- my_set.has(5);
- my_set.forEach(function(value){});
- let my_set = WeakSet() // 只允许对象作为set的元素,便于垃圾回收
Map
- let my_map = new Map();
- let map = new Map([[“name”, “Nicholas”], [“age”, 25]]);
- my_map.set(key, value);
- my_map.get(key)
- my_map.has(key)
- my_map.delete(key);
- my_map.clear();
- my_map.size;
- my_map.forEach(function(value, key, ownerMap) { console.log(key + “ “ + value); console.log(ownerMap === map); });
- let map = new WeakMap();
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 三木的技术博客!
评论