详细见代码演示
function Animal(name){
this.name = name;
}
Animal.prototype.showName = function(){
console.log(this.name);
}
Animal.abc = function(){};
var a = new Animal('Tom');
a.showName();
class Animal{
//静态方法
static showInfo(){
console.log('hi');
}
//构造函数
constructor(name){
this.name = name;
}
showName(){
console.log(this.name);
}
}
let a = new Animal('spike');
a.showName();
//静态方法只能通过类名进行调用
Animal.showInfo();
//类继承extends
class Dog extends Animal{
constructor(name, color){
super(name);//super调用父类
this.color = color;
}
showColor(){
console.log(this.color);
}
}
let d = new Dog('doudou', 'yellow');
d.showName();
d.showColor();
Dog.showInfo();