2
我需要使用setters
洞察力ES6 classes
自動調用一些方法,當一個實例的某些屬性改變。ES6類 - 錯誤的構造函數和setter行爲
我寫了這個:
class Some {
constructor() {
this.xPos = 0;
this.yPos = 0;
}
set xPos (value) {
console.log(`xPos ${this.xPos}`);
}
set yPos (value) {
console.log(`yPos ${this.yPos}`);
}
}
let some = new Some();
但控制檯輸出:
xPos undefined
yPos undefined
所以,如果我對一些道具提供二傳手我必須爲它提供getter? 應該是這樣的: 得到xPos(){return this.xPos} ? – animatio
@animatio如果你想讀取值傳遞給那個setter,你必須 –
@animatio並且在這裏你需要一個屬性,如果你想存儲數據 –