Base Keyword
1 min readRapid 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 usingthisin derived constructors.- Use
super.method()to extend base behavior.