- Hands-On Data Structures and Algorithms with JavaScript
- Kashyap Mukkamala
- 233字
- 2021-06-30 19:12:16
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 WeakMap. As 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.
推薦閱讀
- Vue.js 3.x快速入門
- Java Web開發學習手冊
- PHP 7底層設計與源碼實現
- 實戰Java程序設計
- 精通Python自然語言處理
- LabVIEW虛擬儀器入門與測控應用100例
- HTML+CSS+JavaScript網頁設計從入門到精通 (清華社"視頻大講堂"大系·網絡開發視頻大講堂)
- 精通MySQL 8(視頻教學版)
- Building Serverless Architectures
- Red Hat Enterprise Linux Troubleshooting Guide
- HTML5+CSS3+jQuery Mobile APP與移動網站設計從入門到精通
- Mastering Elixir
- OpenCV 3計算機視覺:Python語言實現(原書第2版)
- 嵌入式Linux C語言程序設計基礎教程
- Learn Linux Quickly