This article analyzes the usage of Javascript accessor attributes for your reference. The specific analysis is as follows:
This is a bit similar to the constructor, but its functions are different. It can make two attributes related and change another attribute by modifying one attribute.
Copy the code as follows: var book = {
_year:2004,
edition: 1
};
Object.defineProperty(book, "year", {
get: function() {
return this._year;
},
set: function(newValue) {
if (newValue > 2004) {
this._year += newValue;
this.edition += newValue - 2004;
}
}
});
book.year = 2006;
console.log(book.edition);
As can be seen from the above example, when modifying _year, only year changes the output value, but through set, the edition also changes when modifying the year.
I hope this article will be helpful to everyone's JavaScript programming.