typestar

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

  1. constructor initializes instance fields.
  2. extends and super build on a base class.
  3. static methods belong to the class, not instances.

Keywords and builtins used here

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.

Type this snippet

Step 1 of 3 in Classes, step 35 of 43 in Language basics.

← Previous Next →