before:
function Animal(name){
this.name = name;
}
Animal.prototype.showName = function(){
console.log(this.name);
}
var a = new Animal('Tom');
a.showName();
var a1 = new Animal('Jerry');
a1.showName();
function Animal(name){
this.name = name;
}
Animal.showInfo = function(){
console.log('hi');
};
Animal.prototype.showName = function(){
console.log(this.name);
}
function Dog(name,color){
Animal.call(this,name);
this.color = color;
}
Dog.prototype = new Animal();
Dog.prototype.constructor = Dog;
Dog.prototype.showColor = function(){
console.log(this.color);
}
let d = new Dog('doudou','yellow');
d.showName();
d.showColor();
now:
class Animal{
// 静态方法(静态方法只能通过类名调用,不可以使用实例对象调用)
static showInfo(){
console.log('hi');
}
// 构造函数
constructor(name){
this.name = name;
}
//实例方法
showName(){
console.log(this.name);
}
}
let a = new Animal('spike');
a.showName();
// a.showInfo();//报错a.showInfo is not a function
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();
// d.showInfo();
Dog.showInfo();