Reputation: 4391
I am using ES5 getters and setters to get DB entries in a way that setting <table>.<entry> = <value>
writes the value to a local cache, and the value is then synchronized with the DB later.
The getter's job is to get the entry from the cache if available, or to pull the result off the DB if not and return that. But it is also supposed to call the setter to cache the result for future queries.
The problem is, I don't know how to call the setter from inside the getter. I have tried this.<entry> = <value>
, <table>.<entry> = <value>
and <entry> = <value>
, all of which result in an error about redeclaring a const (<table>
).
So is there any way of doing this?
EDIT: Here's a minimal example to replicate the issue:
const table = {
_entry: "Something",
get entry(){
/*Do Stuff*/
this.entry = "Something else"; // Exception: SyntaxError: redeclaration of const table
return this._entry;
},
set entry(value){
/*Do Stuff*/
this._entry = value;
}
}
Upvotes: 0
Views: 66
Reputation: 9796
From inside the class(so to say) there is no need to go through getters to access class members. You can just use this
as you suggested. If you are not allowed to do so it means your declaration is forbidding it. Evaluate changing the declaration if you have the need to change the inner state. It is difficult to say more with no code provided.
Upvotes: 1