Why should I do that

In Objects vs. Maps in MDN :

A Map 's keys can be any value (including functions, objects, or any primitive).

I wondered for a long time why should I use objects or functions as the value of the keys… what are the cases in which I have to do this??!

Also :

Why keyObj !== {} and keyFunc !== function () {} as found in Using the Map object (In the last two lines)

finally :innocent: :

Why original !== clone as found in Cloning and merging Maps

Note: Keep in mind that the data itself is not cloned.

I don’t quite understand what that means…

Hi @albaraa_1949

I can’t think of any practical reason. I guess it’s just for completeness and flexibility. I haven’t worked with Maps, yet. Maybe using objects you received as keys for a Map and adding additional information as values.

Even if objects look the same they are different from each other. Same goes for function.

const a = {};
const b = {};
console.log(a===b); // false

For the same reason as above. The clone is a copy of the original Map, but is a different Map. This is useful if you are putting a map (or an object) as parameter to a function. If you modify the map inside the function, the original map gets changed, too. To avoid this you could just pass a clone of the original map into the function.

Have a nice weekend,
Michael

1 Like

Hello @albaraa_1949

imagine a system the represent train system in certain country then we will have multiline and each line has many station so we could use a line as key and linked list of stations as value

for function i do not have an example for that one

and have a nice day both of you :slight_smile:

1 Like