javascript 伪古典继承的快速游乐场

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 伪古典继承的快速游乐场相关的知识,希望对你有一定的参考价值。

function Fruit(attrs) {
  this.type = attrs.type;
  this.name = attrs.name;
  this.isRipe = attrs.isRipe;
  this.calories = attrs.calories;
}
constructor

Fruit.prototype.calculateCalories = function() {
  console.log(`Calories in a ${this.name} are ${this.calories * 200}`);
};

function Banana(banAttrs) {
  Fruit.call(this, banAttrs);
  this.doMonkeysEat = banAttrs.doMonkeysEat;
}

function Kiwi(kiwiAttrs) {
  Fruit.call(this, kiwiAttrs);
  this.isFuzzy = kiwiAttrs.isFuzzy;
}

Banana.prototype = Object.create(Fruit.prototype);
Banana.prototype.ripen = function() {
  if(this.isRipe === false) {
    this.isRipe = true;
  }
};

Kiwi.prototype = Object.create(Fruit.prototype);

const myBanana = new Banana({
  type: 'tree',
  name: 'Banana',
  isRipe: false,
  calories: 1,
  doMonkeysEat: true,
});

const myKiwi = new Kiwi({
  type: 'tree',
  name: 'Kiwi',
  isRipe: false,
  calories: 3,
  isFuzzy: true,
});

console.log(myKiwi);
myKiwi.calculateCalories();
myBanana.calculateCalories();
myKiwi.ripen();

以上是关于javascript 伪古典继承的快速游乐场的主要内容,如果未能解决你的问题,请参考以下文章

选择结构if

让视图总是填满快速的游乐场时间线?

了解JavaScript中的伪数组

快速游乐场找不到任何类型

JavaScript伪数组和数组

AVAudioSourceNode 在快速游乐场中不起作用