Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use memory location as hash key in JavaScript

Say I have a couple of objects that are on the heap:

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

is there a way to put these in hash like so:

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

the only way this might work is if x y and z were represented by their locations in memory. I think this is possible in some languages, is it possible with JS?

like image 394
Alexander Mills Avatar asked Mar 09 '23 15:03

Alexander Mills


1 Answers

Yes, you can do this with an ES6 Map:

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

console.log(c.get(x));
like image 84
Ry- Avatar answered Mar 19 '23 01:03

Ry-