I thought I understood the purpose of get and set functions in accessors. But in a tutorial (W3Schools) they give this example. Notice that the first three are get
and the last two are set
. Why aren't they all set
if we are trying to mutate counter
?
// Define an object
var obj = {counter:0};
// Define Setters and Getters
Object.defineProperty(obj, "reset", {
get : function () {this.counter = 0;}
});
Object.defineProperty(obj, "increment", {
get : function () {this.counter++;}
});
Object.defineProperty(obj, "decrement", {
get : function () {this.counter--;}
});
Object.defineProperty(obj, "add", {
set : function (value) {this.counter += value;}
});
Object.defineProperty(obj, "subtract", {
set : function (value) {this.counter -= value;}
});