1 class關(guān)鍵字、構(gòu)造器和類分開了
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);
}
}