📝 Edit page
➕ Add page
Classes
See also TypeScript Classes cheatsheet.
Resources
- constructor in Mozilla docs.
Basic
class Greeter {
abc = 123
constructor(message) {
this.message = message;
}
greet() {
return `Hello, ${this.message}`;
}
}
const greeter = new Greeter("World");
greet.greet()
// Hello, World!
Extending
class MyClass extends ParentClass {
constructor() {
super();
// ...
}
foo(x) {
// ...
}
}
Object accessors
Setting up setters and getters and a class.
See JavaScript Object Accessors guide.
class Person {
constructor() {
this.language = 'n/a'
}
get lang() {
return this.language
}
set lang(lang) {
this.language = lang
}
}
const p = new Person()
p
// Person { language: 'n/a' }
// Get
p.lang
// 'n/a'
// Set
p.lang = 'English'
p
// Person { language: 'English' }