Working fine with the Person class, the register()
function displays the correct return statement when logged in the console.
However, upon extending it to the Employee class, instead of the expected return statement, the console logs show [Function (anonymous)]
.
Below is the code snippet:
interface PersonInterface {
id: number;
name: string;
register(): string;
}
// Classes
class Person implements PersonInterface {
id: number;
name: string;
constructor(id: number, name: string) {
this.id = id;
this.name = name;
}
register() {
return `${this.name} is now registered`;
}
}
const mike = new Person(2, "Mike Jordan");
class Employee extends Person {
position: string;
constructor(id: number, name: string, position: string) {
super(id, name);
this.position = position;
}
}
const emp = new Employee(3, "Shawn", "Developer");
console.log(emp.register);
The console output for the above code:
[Function (anonymous)]
It appears that the method is not inherited correctly. How can this issue be resolved to ensure that the return statement is displayed as intended, similar to how it functions in the Person class?