Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

73
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda