【发布时间】:2021-05-19 06:03:25
【问题描述】:
class Book{
constructor(title,year){
this.title = title
this.year = year
}
getDetails(){
return `Book ${this.title} is written in ${this.year}`
}
}
class Magazine extends Book{
// constructor(title,year,month){
// super(title,year)
// this.month = month
// }
fullDetails(){
console.log(this.getDetails());
}
}
const newBook = new Magazine('xxx','2022')
newBook.fullDetails() //output : Book xxx is written in 2022
在上面的代码中,Book 是父类,Magazine 是从 Book 类扩展而来的子类。然后我通过传递标题和年份的值为子类创建了一个对象。我已经注释掉了子类(杂志)中的构造函数和超级方法,但仍然初始化了父类属性(标题,年份)的属性,我可以得到输出。任何人都可以在不调用子类中的 super 的情况下解释这是怎么可能的吗?提前致谢。
【问题讨论】:
标签: javascript class prototypal-inheritance