原型链继承:
修改子类的prototype的指向来实现的。
父类的属性和方法,以及父类原型对象上的属性和方法都可以获取到。
继承过来的属性在原型链上。
父类
function Animal(classes){
this.classes=classes;
this.speed=10;
this.body='老鼠';
}
Animal.prototype.eat=function(){
console.log('吃吃吃');
}
子类
function Cat(age,color){
this.age=age;
this.color=color;
}
//通过修改子类的prototype的指向来实现原型链继承。
Cat.prototype=new Animal('猫');
//修改了原型对象,那么原来的constructor属性就没了。所以,需要手动设置一下。
Cat.prototype.constructor=Cat;
var cat=new Cat(1,'blue');
console.log(cat);
console.log(cat.__proto__);//继承的属性和方法在原型对象上