Got this code snippet in JavaScript which defines multiple properties into an object.
var book = {};
Object.defineProperties(book , {
_year: {
value: 2004
},
edition: {
value: 1
},
year: {
get: function() {
return this._year;
},
set: function(newValue) {
if (newValue > 2004) {
this._year = newValue;
this.edition += newValue - 2004;
}
}
}
});
book.year = 2005;
alert(book.edition);
So the book which has that code claims that alert(book.edition);would display 2.Instead , it displays 1. Seems like it never executes the part of the accessors property code (year: get: set:). Anyone knows why this is happening?