Classes
suggest changeSyntax
- class Foo {}
- class Foo extends Bar {}
- class Foo { constructor() {} }
- class Foo { myMethod() {} }
- class Foo { get myProperty() {} }
- class Foo { set myProperty(newValue) {} }
- class Foo { static myStaticMethod() {} }
- class Foo { static get myStaticProperty() {} }
- const Foo = class Foo {};
- const Foo = class {};
Remarks
Classes use JavaScript's prototype-based inheritance model, but they are more than textual shorthand for constructor functions. Class bodies run in strict mode, class declarations have a temporal dead zone, methods are non-enumerable, and a class constructor must be called with new. Modern classes also support public fields, private elements, and static initialization blocks.
class Example {
constructor(value) {
this.value = value;
}
someMethod() {
return this.value;
}
}
const example = new Example(42);
console.log(example.someMethod()); // 42
console.log(example instanceof Example); // true
Managing Private Data with Classes
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents