From what I've gathered from the documentation (here and here), it seems that having a reference to the memory address is necessary for the operation to work:
const foo = {};
const map = new Map();
map.set(foo,'123'); // This action requires knowledge of the memory address of `foo`. Otherwise, any other method would involve converting `foo` into a string.
This limitation exists because keys in JavaScript objects {}
can only be strings (at least in ES5).
However, there appears to be a shim available for Map
: https://github.com/zloirock/core-js#map. I attempted to go through the source code but found it too abstracted (internally, it utilizes strong collection which further imports 10 more files)
Question
Please respond to any of the following queries
- Is there a straightforward method to achieve this without involving string conversion?
- Could it potentially alter
foo
to save a string within it and then utilize that as the key? - Perhaps I'm misunderstanding the documentation entirely?