我有兩個類,environment該類具有站點的屬性,該站點應該具有station該類的多個實體。我正在嘗試在站中添加一個增加方法,該方法將增加站的值并將父環境的掛起值減少相同的數量。我試圖弄亂super,parent和Object.getPrototypeOf,但由于我是 JavaScript OOP(以及 JavaScript 本身)的新手,所以我很掙扎。任何幫助!
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;
}
}
uj5u.com熱心網友回復:
You could try it by adding a reference for the environment to the stations like:
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;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/445959.html
標籤:javascript 节点.js 哎呀
