解决构造函数的缺陷

index.js

function Animal(race) {
  this.race = race;
}

Animal.prototype.eat = function () {
  console.log(`${this.race} is eatting.`);
};

const bird = new Animal("bird");
const dog = new Animal("dog");
bird.eat();
dog.eat();
console.log(bird.eat === dog.eat); // true

posted on 2021-09-10 10:40  aisowe  阅读(60)  评论(0编辑  收藏  举报

导航