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 = new Polygon(); // no error as I had expected.
p.log();
2.class is not hoisted
I also know the reason of this error. class is not hoisted, so this error is my expected result.
const b = new Bolygon(); // Uncaught TypeError as I had expected.
b.log();
class Bolygon {
log() { console.log('i am bolygon'); }
}
question
In some case, such as this code(playground link), will class be hoisted?
I can't understand why new Hero()
doesn't cause error in below.
class Hero
is hoisted?
class AppComponent {
hero = new Hero('foo') // why no error?
}
class Hero {
constructor(public name: string){}
}