无法理解ES2015的新类功能



我很难理解班级功能如何工作。

您可以超级简化以下片段用简单的日常单词?

我真的很难掌握整个事情。

那个超级功能看起来也很奇怪。

预先感谢您。

class Dessert {
  constructor(calories = 250) {
    this.calories = calories;
  }
}
class IceCream extends Dessert {
  constructor(flavor, calories, toppings = []) {
    super(calories);
    this.flavor = flavor;
    this.toppings = toppings;
  }
  addTopping(topping) {
    this.toppings.push(topping);
  }
}

我认为最容易理解的只是创建冰淇淋的新实例:

var iceCream = new IceCream('vanilla'); 
console.log(iceCream) // IceCream {calories: 250, flavor: "vanilla", toppings: Array(0)}

如您所见,您不需要传递卡路里值-super调用父级并进行该值。

当您具有构造函数函数并实例化新实例constructor首先要触发的方法,以收集您在其中描述的属性。

请评论,如果有什么不清楚的话。我很想提供更多信息。

最新更新