1 class关键字、构造器和类分开了
2 class里面直接加方法
老版本写法
function User(name,pass){
this.name=name;
this.pass=pass;
}
User.prototype.showName=function(){
alert(this.name);
}
User.prototype.showPass=function(){
alert(this.pass);
}
var ul=new User('bule','123456');
ul.showName();
ul.showPass();
新版本写法
class User{
constructor(name.pass){
this.name=name;
this.pass=pass;
}
showName(){
alert(this.name);
}
showPass(){
alert(this.pass);
}
}
继承:
super--超类(父类)
老版写法
function VipUser(name,pass,level){
User.call(this,name,pass);
this.level=level;
}
VipUser.prototype=new User();
VipUser.prototype.constructor=VipUser;
VipUser.prototype.showLevel=function(){
alert(this.level);
};
var v1=new VipUser('red','12345','12');
v1.showName();
v1.showPass();
v1.showLevel;
新版写法
class VipUser extends User{
constructor(name,pass,level){
super(name,pass);
this.level=level;
}
showLevel(){
alert(this.level);
}
}
网友评论