继承
class User{
constructor(name,pwd){
this.name=name
this.pwd=pwd
}
showName(){
alert(this.name)
}
showPwd(){
alert(this.pwd)
}
}
var user=new User('LiLei',1233)
user.showName()
user.showPwd()
class VipUser extends User{
constructor(name,pwd,level){
super(name,pwd)
this.level=level
}
showLevel(){
alert(this.level)
}
}
var vip=new VipUser('HanMeiMei',23344,4)
vip.showLevel()
静态方法/属性
const users=[];//该数组将作为User的users属性的初始值
class User{
constructor(firstname,lastname,age){
this.firstname=firstname,
this.lastname=lastname,
this.age=age
}
getName()
{
return `${this.firstname} ${this.lastname}`
}
static insert(firstname,lastname,age)
{
const u=new User(firstname,lastname,age)
User.users.push(u)
return u
}
static getOnebyfirstname(firstname)
{
return Users.find(u=>u.firstname===firstname)
}
//定义静态方法
static list(query){
return User.users
}
//定义静态属性 等效于 static users=users,只是这种写法js暂不支持
static get ['users'](){
return users
}
}
等效于
class User{
constructor(firstname,lastname,age){
this.firstname=firstname,
this.lastname=lastname,
this.age=age
}
getName()
{
return `${this.firstname} ${this.lastname}`
}
static insert(firstname,lastname,age)
{
const u=new User(firstname,lastname,age)
User.users.push(u)
return u
}
static getOnebyfirstname(firstname)
{
return Users.find(u=>u.firstname===firstname)
}
static list(query){
return User.users
}
}
User.users=[];