you can use Object.defineProperty() instead of inline getters:
function A() {};
Object.defineProperty(A.prototype, "x", {
get: function() {
Object.defineProperty(this, "x", {
enumerable: true,
configurable: true,
value: 4
});
return 3;
}
});
var a = new A();
console.log(a.x); // 3
console.log(a.x); // 4
Object.defineProperty() allows us to re-configure the "this" object (the instance, not the proto) with an "own property" primitive value at first run. basically, it dis-ambiguates between this meaning "prototype" and this meaning "own" inside the getter-as-a-method. there might be a way to do that with inline getters, but ODP is more standard anyway, and it actually works as expected.
keep in mind that you can set A.prototype.x to 3, and if needed, ask obj.hasOwnProperty("x") vs if(obj.x) to toggle between local/inherited properties, so you might not even need a getter method...