网上找的视频里学的,当笔记记录一下,并非原创
原写法中类就是构造函数,而ES6中类是类,构造器是构造器,更像Java,更简单明了。
原写法
function User(name,pass){
this.name=name;
this.pass=pass;
}
User.prototype.showName=function(){
console.log(this.name);
}
User.prototype.showPass=function(){
console.log(this.pass);
}
//继承
function VipUser(name,pass,level){
User.call(this,name,pass);
this.level=level;
}
VipUser.prototype=new User();
VipUser.prototype.constractor=VipUser;
VipUser.prototype.showLevel=function(){
console.log(this.level);
}
var u = new User("lidian","123456");
u.showName();
u.showPass();
var v = new VipUser("lidianV","123456","3");
v.showName();
v.showPass();
v.showLevel();
ES6新的写法
class User{
constructor(name,pass){
this.name=name;
this.pass=pass;
}
showName(){
console.log(this.name);
}
showPass(){
console.log(this.pass);
}
}
class VipUser extends User{
constructor(name,pass,level){
super(name,pass);
this.level=level;
}
showLeve(){
console.log(this.level);
}
}
var u = new User("lidian","123456");
u.showName()
u.showPass();
var v = new VipUser("lidianVip","123456","6");
v.showName();
v.showPass();
v.showLeve();
网友评论