Base Keyword

1 min read
Rapid overview

Using super in JavaScript Classes

Use super to call parent class constructors and methods.


Example

class BaseService {
  constructor(protected name: string) {}
  log(message: string) {
    console.log(`[${this.name}] ${message}`);
  }
}

class UserService extends BaseService {
  constructor() {
    super('user');
  }

  fetchUser(id: string) {
    this.log(`fetching ${id}`);
  }
}

Notes

  • super() must be called before using this in derived constructors.
  • Use super.method() to extend base behavior.