Use memory cell as a hash key in JavaScript

Say I have several objects on the heap:

const x = {foo:'bar'};
const y = {foo:'bar'};
const z = {foo:'bar'};

Is there a way to put them in a hash like this:

const c = {x: 'yolo', y: 'rolo', z: 'cholo'};

the only way this can work is: xy and z were represented by their locations in memory. I think this is possible in some languages, is it possible with JS?

+4
source share
1 answer

Yes, you can do this with an ES6 card :

const c = new Map([
    [x, 'yolo'],
    [y, 'rolo'],
    [z, 'cholo'],
]);

console.log(c.get(x));
+7
source

Source: https://habr.com/ru/post/1672130/


All Articles