Richard
Richard

Reputation: 7433

Determine if the constructor is called upon the same element in JavaScript Constructor Design Pattern

I've been using constructor pattern like this:

function Class(parameter) {
  this.id = parameter;
}

let testOne = new Class('a');
let testTwo = new Class('a');

console.log(testOne === testTwo);
console.log(testOne == testTwo);
console.log(testOne.id === testTwo.id);

As you can see, my first console.log(testOne === testTwo) returns false. I think that it has to do with the fact that different instances of a new constructed objects are always different from one another even if it has the same exact properties. Is there another way to check if two objects constructed with the same exact properties other than checking their unique and identifying properties directly?

Upvotes: 2

Views: 67

Answers (2)

Shubham Dixit
Shubham Dixit

Reputation: 1

Internally js has two different approaches to check equality, for primitives (like string) it goes for value comparison and for objects(arrays ,Date object) it goes for reference(That comparison by reference basically checks to see if the objects given refer to the same location in memory.)

Here is an approach to check objects equality by value

function Class(parameter) {
  this.id = parameter;
}

let testOne = new Class('a');
let testTwo = new Class('a');
//console.log(testOne === testTwo);//gives false 
//console.log(testOne == testTwo); // gives false
//
let testThree=testOne;
console.log(testOne === testThree);//gives true (As they both refer to the same instance in memory now)


/// objects equality by value


function isEquivalent(a, b) {
    // Create arrays of property names
    var aProps = Object.getOwnPropertyNames(a);
    var bProps = Object.getOwnPropertyNames(b);

    // If number of properties is different,
    // objects are not equivalent
    if (aProps.length != bProps.length) {
        return false;
    }

    for (var i = 0; i < aProps.length; i++) {
        var propName = aProps[i];

        // If values of same property are not equal,
        // objects are not equivalent
        if (a[propName] !== b[propName]) {
            return false;
        }
    }

    // If we made it this far, objects
    // are considered equivalent
    return true;
}

// Outputs: true
console.log(isEquivalent(testOne, testTwo));

If you feel that method is long and complicated you can try some libraries like lodash it has built in functions for such tasks.

function Class(parameter) {
  this.id = parameter;
}

let testOne = new Class('a');
let testTwo = new Class('a');


console.log(_.isEqual(testOne, testTwo));
// => true
 
console.log(testOne === testTwo);
// => false
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.core.js"></script>

Upvotes: 2

Maheer Ali
Maheer Ali

Reputation: 36584

You can create a function which compare two shallow object.

function Class(parameter) {
  this.id = parameter;
}

let testOne = new Class('a');
let testTwo = new Class('a');

function compObj(obj1,obj2){
    if(Object.keys(obj1).length !== Object.keys(obj2).length) return false;
    for(let key in obj1){
        if(obj1[key] !== obj2[key]) return false;
    }
    return true;
} 
console.log(compObj(testOne,testTwo));

Upvotes: 0

Related Questions