I want to compare two objects with a equals method:
class test{
constructor(name, year) {
this.name= name;
this.year= year;
if (year== 0) {
this.year= -1;
}
}
equals(a){
if(this.year!= a.year){
return false;
}
if(this.name!= a.name){
return false
}
return true;
}
if i call a new Object of the class test:
let a1 = new test("Hey",22);
let a2 = new test("Hey",22);
it works fine for:
console.log(a1.equals(a2)); --> gives me true
but if i generate a new String in the test Object:
let a1 = new test(new String("Hey"),22);
let a2 = new test(new String("Hey"),22);
the output of the equals method gives me false:
console.log(a1.equals(a2)); --> gives me false;
How can i fix the equals method so it also compares a new Object of the type String?