您当前的位置: 首页 > 学无止境 > JS经典实例 网站首页JS经典实例
ES6_class类使用详解
发布时间:2019-11-17 10:47:03编辑:雪饮阅读()
类:
class Person{
//构造方法
constructor(name,age){
this.name=name;
this.age=age;
}
//普通方法
showName(){
console.log(this.name,this.age);
}
}
var p1=new Person('dmj',24);
p1.showName();
继承:
class Person{
constructor(name,age){this.name=name;this.age=age;}
showName(){console.log(this.name,this.age);}
}
class StarPerson extends Person{
constructor(name,age,wife){
super(name,age);
this.wife=wife;
}
}
var p2=new StarPerson('xy',24,'dmj');
p2.showName();
重写
class Person{
constructor(name,age){this.name=name;this.age=age;}
showName(){console.log(this.name,this.age);}
}
class StarPerson extends Person{
constructor(name,age,wife){
super(name,age);
this.wife=wife;
}
showName(){
console.log(this.name,this.age,this.wife);
}
}
var p2=new StarPerson('xy',24,'dmj');
p2.showName();
关键字词:es6,class
下一篇:ES6_字符串,数组的扩展