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

80
Visualizações
Adding equals on primitive object

I'm trying to write a generic method so that I can test equality of two variables, for example:

function shallow_equals(x, y) {
    let
        same_type = typeof x === typeof y,
        is_object = typeof x === 'object';

    if (!same_type) return false;
    if (same_type && !is_object) return x === y;

    // treat it as an object
    if (Object.keys(x).length !== Object.keys(y).length) return false;
    for (key of Object.keys(x)) {
        if (x[key] !== y[key]) {
            return false;
        }
    }

    return true;
}


// shallow comparison of two objects
Object.prototype.equals = function(other) {
    console.log('self ==>', this);
    console.log('other ==>', other);
    console.log('shallow equal?', shallow_equals(this, other));
}

let z = 4;
let w = 4;
z.equals(w);

I'm curious why the above doesn't work, as the two types it gets are:

[Number: 4] --> object
4 --> number

Why does this occur and what would be the proper way to have a function like this?

about 4 years ago · Juan Pablo Isaza
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