মানচিত্র এবং দুর্বলম্যাপের মধ্যে পার্থক্য
ম্যাপ এবং উইকম্যাপের কার্যকরী প্রক্রিয়া একই তবে তাদের মধ্যে সামান্য পার্থক্য রয়েছে।
1) A WeakMap একটি মানচিত্র শুধুমাত্র কী হিসাবে বস্তু গ্রহণ করে ,বস্তু ছাড়াও, আদিম ডেটাটাইপ যেমন স্ট্রিং, সংখ্যা ইত্যাদি গ্রহণ করে।
2) WeakMap অবজেক্ট আবর্জনা সংগ্রহকে এড়াতে পারে না যদি কোন বস্তুর উল্লেখ না থাকে যা একটি চাবির মত কাজ করছে। তাই 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
নীচের উদাহরণে আমরা 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