class person{
constructor(age,sex){
this.age=age
this.sex=sex
this.hobby="running"
this.name="ling"
}
say(word){
alert(word)
}
}
let ling=new person()
ling.say("hello") //alert("hello")
class baby extends person{
constructor(){
super("5months","female") //这里和上面person 类在 constructor 传入的两个参数
this.name="xinxin"
this.food="milk" //constructor 中,可以添加新属性,或者其他代码
alert("这个是baby类")
}
}
let xinxin =new baby()
console.log(xinxin) //constructor 内所有代码被执行;打印台中显示 xinxin 这个对象的内容
console.log(xinxin.age)
console.log(xinxin.food)
console.log(xinxin) console.log(xinxin.age) console.log(xinxin.food)
运行后效果:执行了 constructor中的所有代码(alert("这个是b