Please enable JavaScript to view the comments powered by Disqus.

Object Comparison

A. Preface:

We usually use “==” or “===” to compare variables in JavaScript. But sometimes, the result maybe not as expected. Let’s take a look:

We have class Employee:

function Employee(EmpID, Name, Birthday, Supervisor) {
    this.EmpID = EmpID;
    this.Name = Name;
    this.Birthday = Birthday;
    this.Supervisor = Supervisor;

    this.Age = function () {
        return new Date().getFullYear() - Birthday;
    }
}

 

And some instances of this class:

var Supervisor = new Employee(0, "Marry", 1979);
var Employee1 = new Employee(1, "John", 1980, Supervisor);
var Employee2 = Employee1;
var Employee3 = new Employee(1, "John", 1980, Supervisor);

 

Suppose we want to Employee1  and Employee3  are equal. We can’t use “==” operator or “===” operator.

alert(Employee1 == Employee2); // true. Employee1 and Employee2 are reference to ONE object in memory.
alert(Employee1 == Employee3); // false

 

B. Equal method:

So we’ll define new method to compare object.

    function equals(firstObject, secondObject) {
    var property;

    //Check if current object's property doesn't exist in the second one.
        for (property in firstObject) {
        if (typeof (secondObject[property]) == 'undefined'
        && typeof (firstObject[property]) != 'undefined') {
            return false;
        }
    }

    //Check if second object's property doesn't exist in the current one.
    for (property in secondObject) {
        if (typeof (firstObject[property]) == 'undefined'
        && typeof (secondObject[property]) != 'undefined') {
            return false;
        }
    }

    //Compare properties value in two object
    for (property in firstObject) {
        {
            switch (typeof (firstObject[property])) {
                case 'object':
                //If property is object then call method equals to compare
                if (!equals(firstObject[property], secondObject[property])) {
                    return false;
                } 
                break;

                case 'function':
                    //If property is function then compare content in function
                    if (firstObject[property].toString() != secondObject[property].toString())
                        return false;
                    break;
                default:
                    //Compare value of property
                    if (typeof (firstObject[property]) != typeof (secondObject[property])
                    || firstObject[property] != secondObject[property])
                        return false;

            }
        }
    }
    return true;
}

 

C. Try it

var Supervisor = new Employee(0, "Marry", 1979);
var Employee1 = new Employee(1, "John", 1980, Supervisor);
var Employee2 = Employee1;
var Employee3 = new Employee(1, "John", 1980, Supervisor);

alert(Employee1 == Employee2); // true
alert(Employee1 == Employee3); // false
alert(equals(Employee1, Employee3)); // true

 

D. Source code

ComparingObject.js