美文网首页
ES6: Map, WeakMap

ES6: Map, WeakMap

作者: jerrywu_2b9c | 来源:发表于2017-12-09 09:04 被阅读0次

ES6 中新加入了Map和WeakMap类用于存储 key - value pair。区别于原来的object, Map和WeakMap的key 可以是any value (objects or primitive values), 而 object 的 key 只能是 string 或者 symbol

Map


Create a map

  • let map = new Map([iterable]);
  • iterable
    • An Array or other iterable object whose elements are key-value pairs (arrays with two elements, e.g. [[1, 'one'], [2, 'two']]). Each key-value pair is added to the new Map; null values are treated as undefined

Map v.s. Object

Map Object
key可以是 any value (object or primitive type) key 只能是 String 或者 Symbol
Map 有 size property Object 没有自带的 size property
Map 是一个 iterable, 所以它可以直接用 iterator 来迭代遍历 Object不是iterable, 遍历一个Object需要用一个方法去遍历它的key, 再通过key得到value
Map的key不会跟Map自带的property发生collide Object有prototype property, 可能添加的key会overwrite prototype里边的key。在ES5中可以用 map = Object.create(null) 来创建一个pure empty的object
当add/delete operation比较频繁的时候,Map的performance比较好

Map的Properties和methods

Properties
Map.prototype.size Returns the number of key/value pairs in the Map object.
Methods
Map.prototype.clear() Removes all key/value pairs from the Map object.
Map.prototype.delete(key) Returns true if an element in the Map object existed and has been removed, or false if the element does not exist. Map.prototype.has(key) will return false afterwards.
Map.prototype.entries() Returns a new Iterator object that contains an array of [key, value] for each element in the Map object in insertion order.
Map.prototype.forEach(callbackFn[, thisArg]) Calls callbackFn once for each key-value pair present in the Map object, in insertion order. If a thisArg parameter is provided to forEach, it will be used as the this value for each callback.
Map.prototype.get(key) Returns the value associated to the key, or undefined if there is none.
Map.prototype.has(key) Returns a boolean asserting whether a value has been associated to the key in the Map object or not.
Map.prototype.keys() Returns a new Iterator object that contains the keys for each element in the Map object in insertion order.
Map.prototype.set(key, value) Sets the value for the key in the Map object. Returns the Map object.
Map.prototype.values() Returns a new Iterator object that contains the values for each element in the Map object in insertion order.
Map.prototype[@@iterator]() Returns a new Iterator object that contains an array of [key, value] for each element in the Map object in insertion order.

Useful Example

NaN as key

var myMap = new Map();
myMap.set(NaN, 'not a number');

myMap.get(NaN); // "not a number"

var otherNaN = Number('foo');
myMap.get(otherNaN); // "not a number"

Iterating Maps with for..of

var myMap = new Map();
myMap.set(0, 'zero');
myMap.set(1, 'one');
for (var [key, value] of myMap) {
  console.log(key + ' = ' + value);
}
// 0 = zero
// 1 = one

Relation with Array objects

var kvArray = [['key1', 'value1'], ['key2', 'value2']];

// Use the regular Map constructor to transform a 2D key-value Array into a map
var myMap = new Map(kvArray);

myMap.get('key1'); // returns "value1"

// Use the Array.from function to transform a map into a 2D key-value Array
console.log(Array.from(myMap)); // Will show you exactly the same Array as kvArray

// Or use the keys or values iterators and convert them to an array
console.log(Array.from(myMap.keys())); // Will show ["key1", "key2"]

WeakMap


Create a WeakMap

  • let weakmap = new WeakMap([iterable]);
  • iterable
    • An Array or other iterable object whose elements are key-value pairs (arrays with two elements, e.g. [[1, 'one'], [2, 'two']]). Each key-value pair is added to the new Map; null values are treated as undefined

WeakMap v.s. Map

WeakMap Map
WeakMap的key只能是object type Map的key可以是任何type (object or primitive types)
WeakMap对key object的引用是弱引用,所以garbage collection机制可以将key object回收 Map对key object的引用是strong reference
WeakMap keys are not enumerable Map keys are enumerable. 所以如果要得到一个key 的list的话,应该考虑用Map

WeapMap的properties和methods

Properties
WeakMap.length The value of the length property is 0.
WeakMap.prototype.delete(key) Removes any value associated to the key. WeakMap.prototype.has(key) will return false afterwards.
WeakMap.prototype.get(key) Returns the value associated to the key, or undefined if there is none.
WeakMap.prototype.has(key) Returns a Boolean asserting whether a value has been associated to the key in the WeakMap object or not.
WeakMap.prototype.set(key, value) Sets the value for the key in the WeakMap object. Returns the WeakMap object.

MDN Documents

Reference

JavaScript 内存泄漏教程

相关文章

  • Set 和 Map 数据结构

    Set WeakSet Map WeakMap Set § ⇧ 基本用法 § ⇧ ES6 提供了新的数据结构 Se...

  • ES6 笔记 集合

    ES6 中加入了Set, Map, WeakSet, WeakMap 为什么加入集合 Javascript中的Ob...

  • Set 和 Map 数据结构

    本文介绍 Set、WeakSet、Map、WeakMap 的基本用法 一、Set 1.1、基本用法 ES6 提供了...

  • ES6: Map, WeakMap

    ES6 中新加入了Map和WeakMap类用于存储 key - value pair。区别于原来的object, ...

  • Set WeakSet Map WeakMap

    Set SetWeak Map WeakMap

  • Map、WeakMap、Set、WeakSet

    ES6引入了四种新的数据结构:映射(Map)、集合(Set)、弱集合(WeakSet)和弱映射(WeakMap) ...

  • ES6中的Set和Map

    ES6中新增了Set、WeakSet、Map、WeakMap数据结构 一、Set Set是类似数组的数据结构,和数...

  • WeakMap的学习与应用场景

    WeakMap 是什么? WeakMap 与 Map 类似,也是生成 键值对的组合,但是有区别:1.WeakMap...

  • 『ES6脚丫系列』Set+WeakSet+Map+WeakMap

    『ES6脚丫系列』Set+WeakSet+Map+WeakMap 如果有理解不到位的地方,欢迎大家纠错。 一、Se...

  • 24.WeakMap

    WeakMap WeakMap 是 Map 的弱引用,它也是在 Map 的基础上有了一些限制和自己的特性。 没有 ...

网友评论

      本文标题:ES6: Map, WeakMap

      本文链接:https://www.haomeiwen.com/subject/kaquixtx.html