Alexander Mills
Alexander Mills

Reputation: 100446

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?

Upvotes: 4

Views: 231

Answers (1)

Ry-
Ry-

Reputation: 225263

Yes, you can do this with an ES6 Map:

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

console.log(c.get(x));

Upvotes: 7

Related Questions