I have a weird issue with javascript returning a class object with mixed data.
ie i have created two classes below and when I call the container class to return a “door” it returns an object with part of door1 and door2??
see below snippet I ran in the script console which outputs seen below.
class GarageDoor {
constructor(id) {
this.id = id;
this.item = "GarageDoor" + id;
this.name = "Door" + id;
this.alert = false;
this.opentime = items.getItem(this.item+"_Last_Opened").state;
this.state=items.getItem(this.item).state;
}
output() {
return this.id + ","+ this.item + "="+ this.state +","+ this.name+ ",Alert="+ this.alert+ ",lastopen="+this.opentime ;
}
}
class GarageDoors {
constructor() {
this.door1 = new GarageDoor(1);
this.door2 = new GarageDoor(2);
}
print() {
console.log(this.door1.output());
console.log(this.door2.output());
}
getDoor(name) {
//event.itemName
if (this.door1.item = name) return this.door1;
else if (this.door2.item = name) return this.door2;
else return null;
}
}
console.log("GarageDoor initialisation test");
var mydoors = new GarageDoors();
console.log("getdoor1 "+ mydoors.door1.output());
console.log("getdoor2 "+ mydoors.door2.output());
console.log("GarageDoor getdoor2");
thisdoor = mydoors.getDoor("GarageDoor2");
console.log("getdoor==> item="+ thisdoor.item + " name=" +thisdoor.name);
output:
GarageDoor initialisation test
getdoor1 1,GarageDoor1=CLOSED,Door1,Alert=false,lastopen=2025-03-01T12:40:07.995645218+1100
getdoor2 2,GarageDoor2=CLOSED,Door2,Alert=false,lastopen=2025-02-28T08:42:00.827595585+1100
GarageDoor getdoor2
getdoor==> item=GarageDoor2 name=Door1