Computer >> 컴퓨터 >  >> 프로그램 작성 >> JavaScript

JavaScript에서 Map과 WeakMap의 차이점은 무엇입니까?

<시간/>

Map과 WeakMap의 차이점

Map과 WeakMap의 기능적 메커니즘은 동일하지만 거의 차이가 없습니다.

1) WeakMap 지도 ,객체 외에도 문자열, 숫자 등과 같은 기본 데이터 유형을 허용합니다.

2) 위크맵 객체는 키처럼 작동하는 객체에 대한 참조가 없는 경우 가비지 수집을 방지하지 않습니다. 따라서 WeakMap에서 키를 검색하는 방법이 없습니다. , 반면 지도 키를 가져오는 Map.prototype.keys()와 같은 메서드가 있습니다.

3) WeakMap에 크기 속성이 없습니다. .

지도

문자열, 숫자, 개체 등과 같은 데이터 유형에 관계없이 키를 값에 연결하는 데 사용됩니다.

<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 객체만 허용하고 기본 값(문자열, 숫자)은 허용하지 않음

<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