Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

194
Visualizações
Generate a hash string of an object's reference

I want to produce a hash derived from an Object's reference, and then associate the hash string to the object itself into a dictionary.

I have heard of object-hash and attempted the following implementation:

const objectHash = require('object-hash');


class A {

    a: string;
    constructor() {
        this.a = '1';
    }
}


let a = new A();
let ab = new A();

console.log(objectHash(a)); //     712041d72943c4794bb23d1d455e17b3a4ea17f5
console.log(objectHash(ab));//     712041d72943c4794bb23d1d455e17b3a4ea17f5

In despite of a and ab being different objects (although instances of the same class), the hash produced is equal, which is not the expected result. That is because the library hashes object values rather than object reference.

Expected result

let foo = new A();
let bar = new A();

const fooH = objectHash(foo); <-- Produces a unique hash string representing the reference of "foo"
const barH = objectHash(bar); <-- Produces a unique hash string representing the reference of "bar"


let objects = {
    fooH: foo,
    barH: bar 
}

This could probably be achieved in C++ or C# by using pointers and addresses, but what about Javascript? If anybody can guide me on the right path to incapsulate an object's reference into a string, it would be appreciated.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It is not possible to get an object's reference in JavaScript in order to calculate a hash of it. The reference is an internal value for the engine that is not normally exposed.

However, if you want to index some data against an object, you can use a Map, since the keys can be any arbitrary JavaScript value, thus allowing objects. Since objects are unique during comparison, that means that the same object returns the value:

class A {
    constructor() {
        this.a = '1';
    }
}

const foo = new A();
const bar = new A();

const map = new Map()
  .set(foo, "hello")
  .set(bar, "world");

console.log(map.get(foo), map.get(bar));

JavaScript also has a WeakMap - it only allows objects as keys but in turn it only holds a weak reference to them. This means that once any other reference to the key is gone, the WeakMap will not prevent the object from being garbage collected.

Maps, and especially WeakMaps, are a viable way to extend foreign objects without actually having to nest them in your own hierarchy. Here is a quick demo

import {a, b} from 'foreign';

const extension = new WeakMap()

extension.set(a, { 
    myProperty1: true,
    myProperty2: 40,
    myProperty3: "hello",
});

extension.set(b, { 
    myProperty1: false,
    myProperty2: 2,
    myProperty3: "world"
});

Here we get some foreign objects a, and b we do not control and extend them with our own properties without ever needing to change or clone them. Looking the object in the WeakMap gives the extended properties.

See a more detailed description by Benjamin Gruenbaum


For further reference a Set and a WeakSet exhibit the same properties as a Map and WeakMap, respectively, in terms of storing objects. However, they without an extra value for the object. That can be useful, for example, to keep track of seen objects. Which event handlers have fired, or which nodes in a graph have been visited, and similar.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda