I would expect an instance of the following class to return both the "private" variable AND it's getter, but the getter variable is not returned at all. Why?
class Foo {
_myPrivateVar = null;
get myPublicVar() {
return this._myPrivateVar;
}
set myPublicVar(v) {
if (v > 4) {
this._myPrivateVar = v;
}
}
}
const f = new Foo();
console.log(f) // Foo { _myPrivateVar: 5 }
I expected { _myPrivateVar: 5, myPublicVar: 5 }. I understand I can access f.myPublicVar directly, but the problem is when return the instance from an Express server, for example, the resulting JSON object is devoid of the getter property myPublicVar. Why?