Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

67
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!