I'm trying to build a complex class where I want to group properties, making the instantiated object have multiple layers, instead of every property being at root level. So far, the only way I've found to do this is by making a class with the properties to group, and then in a "parent" class add a property of the class I built. The problem here though is that two properties not sharing the same class can't communicate with each other. There are ways around this, but I find them all very hacky and looking bad. One would be to create a hidden element, and store data in there that a property from another class can read. Another would be to create static properties, but then, unless you do some major work with that property, you can only have one object created from the parent class, as it'll be the same no matter the instantiation of the class.
Very basic example:
class A {
constructor(prop1){
this.property = prop1;
}
}
class B {
constructor(prop2){
this.property = prop2;
}
}
class C {
constructor(prop1, prop2){
this.PropertyA = new A(prop1);
this.PropertyB = new B(prop2);
}
}
let obj = new C(1, 1);
console.log(obj.PropertyA.property);
In this example, the property from class A can't get a value from property in class B.
So, my question is, is there another way of building the class C to keep the levels of hierarchy in the object?
I use the class structure because I like how it looks. It looks far more readable to me than the prototype structure, and I'm not building an object directly, as I would like to instantiate more of them. It feels like I have forgotten things I've looked at to try to do this, but I'm sure it'll come to me soon enough after I post this.