Created
July 3, 2022 18:54
-
-
Save batazo/246f82168b9bd330858f71dceabc9c07 to your computer and use it in GitHub Desktop.
Class static initialization blocks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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