S
S
sanex33392016-03-29 15:18:41
typescript
sanex3339, 2016-03-29 15:18:41

Why is the property of the inherited class undefined inside the implementation of the abstract method?

There is a code. Why does this point to the class in which it is called, and this.name to the parent class? How to fix?

abstract class Abstract {
  protected name: string;
  
  constructor () {
    this.abstractMethod();
  }
  
  protected abstract abstractMethod (): void;
}

class Concrete extends Abstract {
  protected name: string = 'Concrete';
  
  protected abstractMethod () {
    console.log(this, this.name); // Concrete, undefined
  }
}

new Concrete();

Link to example

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sanex3339, 2016-03-29
@sanex3339

I did it differently in the code, but it's still a bug:
https://github.com/Microsoft/TypeScript/issues/7724

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question