We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Classes

Classes in JavaScript are a template for creating objects. As we learned, unlike many other languages, it's easy to create JavaScript objects without classes, but that doesn't mean classes aren't useful.

class User {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }
}

const user = new User("Lane", 100);
  • The class declaration creates a new class
  • The constructor method is a special method that's called when a new instance of the class (object) is created
  • The new keyword calls the constructor method and creates a new instance of the class

Assignment

Textio messages include three properties:

  • recipient: The person receiving the message.
  • sender: The person sending the message.
  • body: The content of the message.

Complete the Message class by adding a constructor that initializes these three properties.