Classes in JavaScript
Class syntax with inheritance and static members.
class Animal {
constructor(name) {
this.name = name;
}
speak() {
return `${this.name} makes a sound`;
}
static create(name) {
return new Animal(name);
}
}
class Dog extends Animal {
speak() {
return `${super.speak()}: a woof`;
}
}
How it works
constructorinitializes instance fields.extendsandsuperbuild on a base class.staticmethods belong to the class, not instances.
Keywords and builtins used here
classconstructorextendsreturnstaticsuperthis
The run, in numbers
- Lines
- 19
- Characters to type
- 233
- Tokens
- 68
- Three-star pace
- 95 tpm
At the three-star pace of 95 tokens a minute, this run takes about 43 seconds.
Step 1 of 3 in Classes, step 35 of 43 in Language basics.