Base Keyword

1 min read
Foundational1 min read
Rapid overview

Base Keyword

TL;DR

super is how a derived class reaches its parent: super(...) invokes the base constructor and super.method() extends inherited behavior. The key rule an interviewer checks is that super() must run before any use of this in a derived constructor, because the base class is responsible for initializing the instance.

How it works


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}`);
  }
}

Additional notes

Notes

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

See also