面向對象:
ES5:this
function User(name,age){
this.name = name;
this.age = age;
}prototype
User.prototype.showName=function(){
console.log(this.name);
}
User.prototype.showAge=function(){
console.log(this.age);
}對象
function Vipuser(name,age,level){
User.call(this,name,age);
this.level = level;
}
Vipuser.prototype = new User();
Vipuser.prototype.constructor = Vipuser;ip
Vipuser.prototype.showLevel=function(){
console.log(this.level);
}io
var v1 = new Vipuser('hum',12,3);
v1.showName();
v1.showAge();
v1.showLevel();
ES6: 面向對象:注意User後面沒有括號。
class User{
constructor(name,age){
this.name = name;
this.age = age;
}
showName(){
console.log(this.name);
}
showAge(){
console.log(this.age);
}
}console
class Vipuser extends User{
constructor(name,age,level){
super(name,age);
this.level = level;
}
showLevel(){
console.log(this.level);
}
}
var v1 = new Vipuser('han',10,2);
v1.showName();
v1.showAge();
v1.showLevel();function