On MDN there is a sample example showing get and set used on an object initialization:
var o = {
property: function ([parameters]) {},
get property() {},
set property(value) {}
};
Without any example showing how to use that structure I always enter in an infinite loop, for example:
var o = {
property: function (test) {return "test"},
get property() {return this.property;},
set property(value) {this.property = value;}
};
When I try to access the object property named property
an infinite loop starts, which I assume is because the get also triggers itself when trying to read the property it gets.
console.log(o.property); // infinite loop occurs
So I am assuming that I am doing this wrong, but in that case the example on that web page is not very clear. The temporary solution I found was to save the value in a similar property (_property
for example), but I would like to not do this and keep the property name intact for the object, getter and setter.
Is this possible and if so how to do it correctly?