Tengo dos clases, la clase de environment tiene una propiedad de estaciones que se supone que tiene varias instancias de la clase de station . Estoy tratando de agregar un método de aumento en la estación que aumentará el valor de la estación y disminuirá el valor pendiente del entorno principal en la misma cantidad. He intentado jugar con super , parent y Object.getPrototypeOf , pero como soy nuevo en JavaScript OOP (y en JavaScript en sí mismo), tengo problemas. ¡Alguna ayuda!
class enviroment { constructor(name) { this.name = name; this.pending = 0; this.stations = []; } newStation(value = 0, name = null) { this.stations.push(new station(value, name)); return this; } } class station { constructor(value = 0, label = null) { this.value = value; this.label = label; this.isTaken = false; } increase(increasment) { this.value += increasment; this.parent.pending -= increasment; // <---- HERE return this; } }Puede intentarlo agregando una referencia para el entorno a las estaciones como:
class enviroment { constructor(name) { this.name = name; this.pending = 0; this.stations = []; } newStation(value = 0, name = null) { this.stations.push(new station(value, name, this)); return this; } } class station { constructor(value = 0, label = null, environment = null) { this.value = value; this.label = label; this.isTaken = false; this.environment = environment; } increase(increasment) { this.value += increasment; if(this.environment) this.environment.pending -= increasment; // <---- HERE return this; } }