Следующий код выдаст предупреждение undefined
class Parent {
field: string
constructor() {
alert(this.field)
}
}
class Child extends Parent {
field = 'child'
}
new Child() #=> undefined
, тогда как следующие предупреждения 'child' ожидаются
class Parent {
field: string
constructor() {
alert(this.field)
}
}
class Child extends Parent {
field = 'child'
constructor() {
// without referencing this.field before super(), this.field stays undefiend
this.field
super()
}
}
new Child() #=> 'child'
Есть ли способы выполнить следующие условия?
- опустить полное объявление конструктора Child, как в первом примере
- захватить переменную-член в классе Child?