class engineer {
constructor(public lang: string, private _salary: number) {}
// get 可以保護私有且真正的數字 並動手腳做處理再暴露出來
get income() {
return this._salary - 2000
}
set income(salary: number) {
const realIncome = salary + 500
this._salary = realIncome
}
}
const SWE = new engineer('java', 6000)
console.log(SWE.income)
老師這是我寫的範例:
我預想的結果是 6000 進入 set 後會先 進入set +500 並塞進真正的私有變數
然後 透過get income log出來4500的數字
但我log出來的結果是 4000 我漏了什麼嗎?