node中的声明类和类的继承
//js中的构造函数
function Animal(name){
this.name=name;
}//属性
Animal.prototype.showName(){
console.log(this.name);
}//方法
var a=new Animal('Tom');
a.showName();
//node中的构造函数是类
class Animal{
static showInfo(){
console.log('hi');
}//声明一个静态方法
constructor(name){
this.name=name;
}//属性
showName(){
console.log(this.name);
}//动态方法方法
}
let a = new Animal('Jerry');
a.showName();//动态方法调用
Animal.showInfo();//静态方法只能通过类名访问
继承:extends,super(),
//类的继承
class Dog extends Animal{
constructor(name,color){
super(name);//继承父类中的name属性
this.color=color;//又有自己的属性
}
showColor(){
console.log(this.color);
}//自己的方法
}
let b = new Dog('doudou','yellow');
b.showName();
b.showColor();
Dog.showInfo();//静态方法通过类名调用。