官术网_书友最值得收藏!

API differences

The API for Map and WeakMap is very similar when it comes to standard operations, such as set() and get(). This makes the API very straightforward and contains the following:

  • Map.prototype.size: Returns the size of the map; not available on typical objects unless you loop and count
  • Map.prototype.set: Sets a value for a given key and returns the entire new map
  • Map.prototype.get: Gets a value for a given key and returns undefined if not found
  • Map.prototype.delete: Deletes a value for a given key and returns true if deletion was successful, otherwise false
  • Map.prototype.has: Checks the map for the presence of an element with the key provided; returns boolean
  • Map.prototype.clear: Clears the map; returns nothing
  • Map.prototype.forEach: Loops over the map and gives access to each element
  • Map.prototype.entries: Returns an iterator on which you can apply the next() method to get the value of the next element in Map, for example, mapIterator.next().value
  • Map.prototype.keys: Similar to entries; returns an iterator that can be used to get access to the next value
  • Map.prototype.values: Similar to key; returns access to values

The main difference comes in when accessing anything related to keys and the values for a WeakMapAs described earlier, because of the enumeration challenge in case of the WeakMap, methods such as size(), forEach(), entries(), keys(), and values() are not available in  WeakMap.

主站蜘蛛池模板: 儋州市| 隆化县| 荣成市| 呼和浩特市| 普陀区| 密云县| 洪雅县| 永丰县| 洪洞县| 弥勒县| 体育| 赤壁市| 竹北市| 沅江市| 平山县| 德庆县| 井陉县| 阿城市| 天长市| 永川市| 大荔县| 中宁县| 额敏县| 塘沽区| 克东县| 驻马店市| 长武县| 庆云县| 社旗县| 洞口县| 资中县| 济源市| 广德县| 高平市| 彩票| 陇川县| 大新县| 云梦县| 英德市| 依兰县| 巫溪县|