ES6 Everyday: Classes

After a week hiatus, we’re back!

And, starting off this week, hold on to your pants: JavaScript has classes!

class Dog
{
  bark()
  {
    console.log("Bark!");
  }
}

var spot = new Dog();
spot.bark();

Yup, and you get constructors too:

class Person
{
  constructor(firstName, lastName)
  {
    this.firstName = firstName;
    this.lastName = lastName;
  }
  
  getFullName()
  {
    return this.firstName + " " + this.lastName;
  }
}

var person = new Person("Logan", "Franken");
console.log(person.firstName); // Logan
console.log(person.getFullName()); // Logan Franken

Check it out for yourself in this ES6 fiddle.

Resources