JS/TS extending classes by passing in instance?

Viewed 21

Is there any way to do something as follows: Rather than call super on the properties of the base class, just pass in an instance of the base class? I am getting an error in my editor when trying but wondering if I am missing something if it's just not allowed. Do I really have to pass in each property of the extended class if I already have an instance of the base class?

class Person {
  private name: string
  private age: number
  constructor(name: string, age: number) {
    this.name = name;
    this.age = age;
  }
}


class Employee extends Person {
  private position: string;
  //pass in the instance of Person already created
  constructor(person: Person, position: string) {
    super(person);
    this.position = position;
  } 
}

let philInstance = new Person("Phil", 20);
let janitor = new Employee(philInstance, "janitor")
0 Answers
Related