class Person { constructor(firstName, lastName) { this.firstName = firstName; this.lastName = lastName; } get fullName() { return `${this.firstName} ${this.lastName}`; } set fullName(name) { name = name.split(' '); this.firstName = name[0]; this.lastName = name[1]; } } class Employee extends Person { constructor(firstName, lastName, title, salary) { super(); this.title = title; this.salary = salary; } get employeeInfo() { return `${this.fullName}, ${this.title}, earns $${this.salary} annually.`; } } const employee = Employee('Jane', 'Doe', 'Software Developer', 90000);
class Person { constructor(firstName, lastName) { this.firstName = firstName; this.lastName = lastName; } get fullName() { return `${this.firstName} ${this.lastName}`; } set fullName(name) { name = name.split(' '); this.firstName = name[0]; this.lastName = name[1]; } } class Employee extends Person { constructor(firstName, lastName, title, salary) { super(); this.title = title; this.salary = salary; } get employeeInfo() { return `${this.fullName}, ${this.title}, earns $${this.salary} annually.`; } } const employee = Employee('Jane', 'Doe', 'Software Developer', 90000);
There’s a bunch of issues (and probably some others I don’t mention).
First, we’re not validating any types (yes, I hear TypeScript fixes this). Second, it’s probably not a great idea to mutate our name parameter in set fullName. Third, we need to pass firstName and lastName to super inside of our Employee constructor. Fourth, because of automatic semicolon insertion, our employeeInfo will return undefined instead of our string. And last (I think), when we create a new instance of Employee, we need to do so with the new keyword.