JavaScript Map和WeakMap有什么区别

JavaScript Map和WeakMap有什么区别

在这篇文章中,我们将讨论ES6引入的Map和WeakMap之间的区别。JavaScript对象只支持一个键对象,为了支持多个键对象,Map就应运而生。

Map: Map是一个无序的键值对列表,其中键和值可以是任何类型,如字符串、布尔值、数字等。为了更好地理解,我们来看一个Map和它的属性的示例。

示例: 这个示例展示了在JavaScript中实现Map。

Javascript

// Creating an empty map
const myMap = new Map();
 
// Creating a set by inserting the key-value pair
console.log(myMap);
 
myMap.set("info", { name: "Sam", age: 36 });
 
// Access the elements of a Map
console.log(myMap);
 
console.log(myMap.get("info"));
 
// Checking the element in a Map using has() method
console.log("check whether info is there or not - "
    + myMap.has("info"));
 
// Returning the number of elements using size property
console.log("The no.of elements in a Map are " + myMap.size);
 
// Removing the element from the map using
// clear() and delete() methods
// removing a particular element
myMap.delete("address");
 
myMap.delete("info"); // true
console.log(myMap);
 
// Iteration through the map
// using forEach method()
const map2 = new Map();
map2.set("name", "Sam");
map2.set("age", "36");
 
// looping through Map
map2.forEach(function (value, key) {
    console.log(key + "- " + value);
});

输出

Map(0) {}
Map(1) { 'info' => { name: 'Sam', age: 36 } }
{ name: 'Sam', age: 36 }
check whether info is there or not - true
The no.of elements in a Map are 1
Map(0) {}
name- Sam
age- 36

WeakMap : 在 WeakMap 中,每个键值只能是对象或函数。它用于存储弱对象引用。为了更好的理解,我们来看一个 WeakMap 的示例和它的属性:

示例: 这个示例展示了在 Javascript 中实现 Map 的方式。

Javascript

// Creating a WeakMap
const myweakMap = new WeakMap();
console.log(myweakMap); // WeakMap {} 
 
let obj = {};
 
// Adding object (element) to WeakMap
myweakMap.set(obj, 'hello everyone');
 
console.log(myweakMap);
 
// Access the element of a WeakMap using get() method
console.log("The element of a WeakMap - " + myweakMap.get(obj));
 
// Checking the element in a map using has() method
console.log("check if an element is present in WeakMap - "
    + myweakMap.has(obj));
 
// Delete the element of WeakMap using delete() method
console.log("deleting the element of WeakMap - "
    + myweakMap.delete(obj));
console.log(myweakMap); // WeakMap {}
 
// WeakMaps are not iterable. It will return
// an error. For example,
const weakMap1 = new WeakMap();
console.log(weakMap1); // WeakMap {} 
let obj1 = {};
 
// Adding object (element) to WeakMap
weakMap.set(obj1, 'hello');
 
// Looping through WeakMap
for (let i of weakMap1) {
 
    console.log(i);  // TypeError
}

输出:

WeakMap { <items unknown> }
WeakMap { <items unknown> }
The element of a WeakMap - hello everyone
check if an element is present in WeakMap - true
deleting the element of WeakMap - true
WeakMap { <items unknown> }
WeakMap { <items unknown> }

ReferenceError: weakMap is not defined

Map和WeakMap之间的区别:

Map WeakMap
Map是一个无序的键值对列表,其中键和值可以是任何类型,例如字符串、布尔值、数字等。 在WeakMap中,每个键只能是对象和函数。它用于存储弱引用对象。
Map是可迭代的。 WeakMap不可迭代。
即使不使用,Map也会保留所有内容。 WeakMap只保留键的引用,而不是键本身。
垃圾收集器不会从“Map”中移除键指针,也不会将键从内存中移除。 垃圾收集器会从“WeakMap”中移除键指针,并将键从内存中移除。WeakMap允许垃圾收集器完成其任务,但Map不行。
Map有一些属性:.set、.get、.delete、.size、.has、.forEach、迭代器。 WeakMap有一些属性:.set、.get、.delete、.has。
您可以使用 new Map() 创建一个新的Map。 您可以使用 new WeakMap() 创建一个新的WeakMap。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程