Last active
February 17, 2025 23:07
-
-
Save Offirmo/7043e258a57e57ffe3bd4dba560a180b to your computer and use it in GitHub Desktop.
[🔷TS -- classes] #typescript
This file contains 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
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes | |
// https://www.typescriptlang.org/docs/handbook/2/classes.html | |
class Foo { | |
#privateFieldWithInitializer = 42; | |
constructor(...args) { | |
super(...args) | |
this.code = "42" | |
} | |
method(id: AppId) { // not bound | |
this.privateFieldWithInitializer = id | |
} | |
readonly method = (id: AppId) { // bound! | |
this.privateFieldWithInitializer = id | |
} | |
static #privateStaticField; | |
static #privateStaticFieldWithInitializer = 42; | |
static #privateStaticMethod() { | |
// … | |
} | |
static { | |
// Class Static Initialization Blocks https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Static_initialization_blocks | |
this.staticProperty2 = 'Property 2'; | |
} | |
} | |
class Foo { | |
constructor(private readonly privateField: bar) {} // equivalent to declaring + initializing a private field | |
} | |
// implements, extends | |
// https://www.typescriptlang.org/docs/handbook/classes.html | |
class Greeter { | |
greeting: string; | |
constructor(message: string) { | |
this.greeting = message; | |
} | |
greet() { | |
return "Hello, " + this.greeting; | |
} | |
} | |
let greeter = new Greeter("world"); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment