Skip to content Skip to sidebar Skip to footer

Typescript Class: Will Class Be Hoisted When I Use Two Classes?

background 1.class should be defined in advance I know there is no error because class is defined in advance. class Polygon { log() { console.log('i am polygon'); } } const p = n

Solution 1:

The reason is that this line:

hero = new Hero('foo')

Is only evaluated when you instantiate AppComponent which is after the class Hero has been evaluated. However, in your 2nd code snippet this line:

const b = newBolygon();

Is evaluated first, before the class itself has been evaluated.

If you make AppComponent.hero static it will cause an error:

classAppComponent {
  static hero = new Hero('foo') // errro: Hero is not a constructor
}

Post a Comment for "Typescript Class: Will Class Be Hoisted When I Use Two Classes?"