通過getter進行對象屬性動態設置

var tom={
    name:'Tom',
    _birth:1999,
    age:undefined
};

Object.defineProperty(tom,'age',{
    get:function (){
		let d=new Date();
		let year=d.getFullYear();
		let age=year-this._birth;
        return age;
    }
})

tom.age;    //19

仍有一個問題,這個tom對象中的age屬性,加不加不影響效果,所以到底要不要呢?

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章