Class Preview

Class Preview

Here's a quick peek of what a JavaScript class look like:

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);
  }
}

Notice the newclasskeyword right in front ofDessertandIceCream, or the newextendskeyword inclass IceCream extends Dessert? What about the call tosuper()inside the IceCream'sconstructor()method.

There are a bunch of new keywords and syntax to play with when creating JavaScript classes. But, before we jump into the specifics of how to write JavaScript classes, we want to point out a rather confusing part about JavaScript compared with class-based languages.

results matching ""

    No results matching ""