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

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.

主站蜘蛛池模板: 屏山县| 资阳市| 宁津县| 启东市| 新田县| 南充市| 尼勒克县| 沧州市| 团风县| 济阳县| 樟树市| 太仓市| 巴彦淖尔市| 登封市| 乌拉特后旗| 大化| 大港区| 克拉玛依市| 岳阳市| 大城县| 枣强县| 浦城县| 通江县| 迁安市| 庆云县| 自治县| 新郑市| 武功县| 大城县| 浦江县| 深水埗区| 禹城市| 新丰县| 阿勒泰市| 吉林省| 襄城县| 南岸区| 龙泉市| 凯里市| 德令哈市| 湾仔区|