4 ms·
"Any type of value as key" is quite misleading since anything other than primitive types (number, bool, string) is compared by reference. Observe: > const m
by IshKebab 6y ago
"Any type of value as key" is quite misleading since anything other than primitive types (number, bool, string) is compared by reference. Observe:
> const m = new Map();
undefined
> m.set([1], "one");
Map(1) {Array(1) => "one"}
>m.get([1])
undefined
C++ and Rust don't stand for this nonsense. You can't even define a custom hashing function, which means if you want a useful map for anything other than numbers and strings you have to write a wrapper around `Map` that hashes the objects to numbers or strings, and then you're more or less back to raw objects.
It's definitely better than raw objects because it doesn't mix up data and methods, but it's not much better.
- gilnaa 6y agoI was quite surprised to read that you can't set a custom hasher in C++/Rust, and I even went and made sure that it's possible. But then I re-read your comment and realized you've meant that you can't do this in JS.
- temporarysdwvit 6y agoOh you can, just implement toString and js will use it for hashing