类机制
面向对象:
类(es6)-->构造函数(es5)
1>空类
class student{ (class后跟类名)
//不写constructor(),默认提供一个空构造器
constructor(){
this.proto->prototype
this->new
}
}
var stu1=new student();
2>构造函数
class student{
constructor(name,age){
//构造器,专门用来设给实例对象
this.name=name;
this.age=age;
gender:‘men’
}
}
var stu1=new student(“tom”,20);
3>原型对象的方法/非静态方法
直接写在类体即可
class student{
constructor(){}
say(){} //student.prototype.say
}
4>静态方法
声明在构造函数内部,只能构造函数本身调用
class studengt{
//静态方法
static num=10;
//构造器
constructor(){};
//非静态方法
say (){}
}
继承:
extends关键字↓
class poor extends rich{ //继承rich类
constructor(){
super(name,age);
this.gender=gender;
}
}
var p1=new poor(“lisi”,“20”,“men”)
子类如果不写构造器,默认继承父类的构造器
如果自己重写构造器,必须调用super()