Skip to content

Instantly share code, notes, and snippets.

@batazo
Created July 3, 2022 18:54
Show Gist options
  • Save batazo/246f82168b9bd330858f71dceabc9c07 to your computer and use it in GitHub Desktop.
Save batazo/246f82168b9bd330858f71dceabc9c07 to your computer and use it in GitHub Desktop.
Class static initialization blocks
class User {
isActive = false;
get getStatus(){
if(!this.#isActive){
throw new Error('User is not active');
}
return this.#isActive;
}
}
// Evaluate outside the class body
try {
const state = User.isActive;
User.isActive = state
} catch {
User.isActive = false
}
//------------------------------------------------------------------------
let initialState;
class User {
#isActive = false;
get getState(){
if(!this.#isActive){
throw new Error('User is not active');
}
return this.#isActive;
}
static {
initialState = () => {
this.#isActive = this.getState;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment