<script type="text/javascript">
//父类
class P1{
constructor(opt){
this.x = opt.x
}
say(){
console.log(this.x)
}
}
class P2 extends P1{
constructor(opt){
super(opt)//super必须调用,这是语法规定,否则会把错
//扩展方法
this.y = opt.y
}
hi(){
console.log(this.y)
}
}
const o1 = new P1({x:0});
const o2 = new P2({x:1,y:2});
o1.say();//0
o2.say();//1
o2.hi();//2
</script>
网友评论