ES5实现一个类
function User(name,age){
this.name=name;
this.age=age;
}
User.prototype.getName=function(){
return this.name;
}
User.prototype.getAge=function(){
return this.age;
}
//Student类继承User类
function Student(name,age,university){
User.call(this,name,age);
this.university=university;
}
Student.prototype=new User();
Student.prototype.constructor=Student;
Student.prototype.getUniversity=function(){
return this.university;
}
let user=new User('肖敏',18);
let student=new Student('刘阔',23,'A大学')
ES6实现一个类
class User{
constructor(name,age){
this.name=name;
this.age=age;
}
getName(){
return this.name;
}
getAge(){
return this.age;
}
}
//Student类继承User类
class Student extends User{
constructor(name,age,university){
super(name,age);
this.university=university;
}
getUniversity(){
return this.university;
}
}
let user=new User('肖敏',18);
let student=new Student('刘阔',23,'A大学')