Targeting ES2022, you can achieve transient variables at least in 2 ways:
- Using a private variable
- Using an "accessor" variable
"use strict";
class Example {
constructor() {
this.#aTransientValue_accessor_storage = "transient";
this.aNormalValue = "public";
this.#aPrivateValue = "public";
}
#aTransientValue_accessor_storage;
get aTransientValue() { return this.#aTransientValue_accessor_storage; }
set aTransientValue(value) { this.#aTransientValue_accessor_storage = value; }
#aPrivateValue;
}
const example = new Example();
console.log(example); /// prints { "aNormalValue": "public" }
Typescript even introduced the "accessor" keyword, which can be used for this purpose.
Here is the TS equivalent code:
class Example {
accessor aTransientValue = "transient";
public aNormalValue = "public"
#aPrivateValue = "public"
}
const example = new Example();
console.log(example);