5

I have one class called tax.

 export class tax { private _id: string; private _name: string; private _percentage: number;` constructor(id: string = "", taxName: string = "", percentage: number = 0) { this._id = id; this._name = taxName; this._percentage = percentage; } public get id(): string { return this._id; } public set id(v: string) { this._id = v; } public get name(): string { return this._name; } public set name(v: string) { this._name = v; } public get percentage(): number { return this._percentage; } public set percentage(v: number) { this._percentage = v; } toString(){ return this.id; } } 

When I create 2 different objects of this class

 a1: tax = new tax("id","name",4); a2: tax = new tax("id","name",4); console.log(a1 === a2); //false console.log(a1 == a2); //false 

When I give a1 === a2 it should give true. what changes i have to do in my class so it will give a1 === a2 ? What I have to do in tax class? or which method I have to override in tax class?

6
  • "it should give true" not it should not. it will be true only for same object, so object and its reference. not for two different objects like in your case. because you construct two new objects, they are not the same, even if they might look similar. Commented Apr 26, 2017 at 12:28
  • my question is **what changes i have to do in my tax class when i create two different object with same value THEN ** i compare both object it will give me true. Commented Apr 27, 2017 at 4:53
  • You can't do anything so that === give you true for two different objects. You could however, do it smart and turn Tax class into some sort of Singleton variation when it would not construct new object for same parameters but return previous one. Then a1 === a2 would be true. Commented Apr 27, 2017 at 8:11
  • If you're okay with using lodash, it provides a method for deep comparison between objects. Just compare with _.isEqual(a1, a2). Docs can be found here: lodash.com/docs/4.17.5#isEqual Commented Mar 14, 2018 at 21:00
  • The thing to remember here is that you are checking equality of the reference (memory location) but you are wanting to do a value comparison. Commented Jun 2, 2020 at 16:54

1 Answer 1

4

You're comparing two different instances of the same class. Even though the values are the same within the instance, they're two completely separate entities.

For example, if we were to have a People class with a single attribute name. Even if there are two people in the world called John Smith, they're two completely separate people.

Each instance has its own unique identifier.

If you want to check if two taxes have the exact same values, you could check them one by one.

console.log(a1.getId() === a2.getId() && a1.getName() === a2.getName() && a1.getPercentage() === a2.getPercentage()) // true 

More information about comparing objects can be found here

Sign up to request clarification or add additional context in comments.

1 Comment

I used this one with success (to avoid writing manual comparisons): npmjs.com/package/deep-equal

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.