JavaScript 中 Map 和 WeakMap 的區別是什麼?
Map 和 WeakMap 的區別
Map 和 WeakMap 的功能機制相同,但它們之間存在一些細微差別。
1) WeakMap 僅接受物件作為鍵,而Map 除了物件之外,還接受基本資料型別,例如字串、數字等。
2) 如果沒有對充當鍵的物件的引用,WeakMap 物件不會阻止垃圾回收。因此,在WeakMap 中沒有方法可以檢索鍵,而在Map 中則存在諸如 Map.prototype.keys() 之類的方法來獲取鍵。
3) WeakMap 中不存在 size 屬性。
Map
它用於將鍵與值關聯起來,無論資料型別如何,例如字串、數字、物件等。
示例
<html>
<body>
<script>
// Creates a new Map object
var map = new Map();
// Defines an object that will be used a key in the ma
var objKey = {name: 'tutorialspoint'};
document.write("</br>");
// Adds a new element having a String as its key and a String as its value
map.set('first', 'a');
document.write("</br>");
// Adds a new element having a Number as its key and an Array as its value
map.set(3, ['c']);
document.write("</br>");
// Adds a new element having an Object as its key and a Number as its value
map.set(objKey, 3);
// Adds a new element having an Array as its key and a String as its value
map.set(['add', 'mapping'], 'd');
// Checks whether an element having a key of "2" exists in the map.
document.write(map.has(2));
document.write("</br>");
// Checks whether an element having a key of "first" exists in the map.
document.write(map.has('first'));
document.write("</br>");
// Retrieves the element having key of "first". Prints "a"
document.write(map.get('first'));
document.write("</br>");
// Retrieves the element having as a key the value of objKey.
document.write(map.get(objKey));
document.write("</br>");
// Retrieves the element having key of "empty". Prints "undefined"
document.write(map.get('empty'));
document.write("</br>");
// Retrieves the map size. Prints "4"
document.write(map.size);
document.write("</br>");
// deletes all the value
map.clear();
document.write(map.size);
</script>
</body>
</html>輸出
false true a 3 undefined 4 0
WeakMap
在下面的示例中,我們可以發現WeakMap 僅接受物件,而不接受任何基本值(字串、數字)。
示例
<html>
<body>
<script>
// Creates a new WeakMap object
var weakMap = new WeakMap();
// Defines an object that will be used a key in the map
var obj4 = {d: 4};
// Defines another object that will be used a key in the map
var obj5 = {e: 5};
// Adds a new element having an Object as its key and a String as its value
weakMap.set(obj4, 'fourth');
// Adds a new element having an Object as its key and a String as its value
weakMap.set(obj5, 'fifth');
// Adds a new element having a Function as its key and a Number as its value
weakMap.set(function(){}, 7);
// Checks whether an element having as its key the value of objKey4 exists in the weak map.
document.write(weakMap.has(obj4));
document.write("</br>");
// Retrieve the value of element associated with the key having the value of objKey4. Prints "first"
document.write(weakMap.get(obj4));
document.write("</br>");
// Deletes the element having key of objKey4. Prints "true"
document.write(weakMap.delete(obj4));
document.write("</br>");
// Deletes all the elements of the weak map
weakMap.clear();
</script>
</body>
</html>輸出
true fourth true
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP