所以我已經知道在Javascript中我們可以使用defineProperties來定義對象的多個屬性。因此,我已經在下面的簡單代碼中嘗試了它,但我並不清楚得到我想要的結果。這似乎是訪問者不工作,我不知道爲什麼。JavaScript對象defineProperties不起作用
var book = {};
Object.defineProperties(book,{
_year: {
value: 2004 },
edition: {
value: 1},
year: {
get: function(){
this._year;},
set: function(value){
if(value>2004){
this._year = value;
this.edition = this.edition + value - 2004;
});
this.year = 2016;
alert(book.edition); //1 why??
你似乎缺少一些'}' – j08691
是的,我只是意識到我錯過了那些右括號,格式化風格並沒有說清楚。謝謝 – Celaro