JS继承
JS作为面向对象的弱类型语言,继承也是其非常强大的特性之一
JS继承分为: 原型继承、构造继承、实例继承、拷贝继承、组合继承、寄生组合继承、 __proto__继承
1.原型继承:
function Animate(name) {
this.name = name || 'cat';
this.sleep = function () {
console.log(this.name + '正在睡觉');
};
}
Animate.prototype.eat = function (food) {
console.log(this.name + '正在吃' + food);
};
//-------->>原型继承
function Cat() {
}
Cat.prototype = new Animate();
Cat.prototype.name = 'cat';
var cat = new Cat();
console.log(cat.name);
console.log(cat.eat('fish'));
console.log(cat.sleep());
console.log(cat instanceof Animate);
console.log(cat instanceof Cat);