// 原型继承
// 缺陷
//1:如果父类有引用类型(对象、数组、函数)的属性,其中一个实例修改了这个属性,所有的实例上的这个属性都会被修改。
//2:实例无法向其构造函数传递参数
const A = function () {
function Animal() {
this.species = '动物';
this.friend = ['cow']
}
function Cat() {
this.eat = 'fish'
}
Cat.prototype = new Animal();
Cat.prototype.constructor = Cat;
let smallCat = new Cat();
let middleCat = new Cat();
console.log(smallCat.species, middleCat.species);//动物
smallCat.species = '猫咪';
smallCat.friend.push('dog');
console.log(smallCat.friend, middleCat.friend);
}
// Class继承
const B = function () {
class Animal {
constructor() {
this.species = '动物';
this.friend = ['cow']
}
}
class Cat extends Animal {
constructor() {
super();
this.eat = 'fish'
}
}
let smallCat = new Cat();
let middleCat = new Cat();
smallCat.friend.push('dog');
console.log(smallCat.friend, 'smallCat.friend');
console.log(middleCat.friend, 'smallCat.friend')
}
B()
console