This code snippet is supposed to display 'Hello World!' on the screen, but instead it shows an error message:
Error: Cannot read property 'id' of undefined
. What could be the reason behind this issue?
abstract class Parent {
constructor() {
console.log(this.getData());
}
abstract getData(): string;
}
class Child extends Parent {
constructor(private item: any) {
super();
}
getData(): string {
return this.item.id;
}
}
new Child({id: 'Hello World!'});
Click here for a functional example.