Classes

I. Javascript Classes

Classes make creating multiple objects that share property names and methods easy.

II. Define a class

To create a new class, we use the class keyword. The class keyword is followed by a CamelCased class name.

class Person {
    // Code
}

III. Create new instance

An instance is an object that contains a class’s property names and methods but with unique property values.

For example, to create a person, we use the keyword new.

const person1 = new Person();

IV. Constructor

The constructor() method is called when we create a new class instance. It sets the property values for each instance.

Remember that a class has only 1 constructor(). Writing two constructors() will give a SyntaxError error.

For example, if we want our Person class to have properties such as firstName, lastName, birthDay, we will put them in the constructor, and our properties will be loaded by the constructor first for other methods to use.

class Person {
  constructor(firstName, lastName, birthDay) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.birthDay = new Date(birthDay);
  }
}

V. Methods

We can add functions in classes easily.

class Person {
  constructor(firstName, lastName, birthDay) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.birthDay = new Date(birthDay);
  }

  getFullName() {
    return `${this.firstName} ${this.lastName}`;
  }

  getBYear() {
    return this.birthDay.getFullYear();
  }
}

// Object
const p1 = new Person("Anna", "Wood", "3-25-1995");
const p2 = new Person("Joe", "Hopkins", "10-12-1990");

console.log(p2.getFullName()); // Joe Hopkins
console.log(p1.getBYear()); // 1995

VI. Subclass inheritance

A subclass is a class that inherits both the properties and behaviors of the parent’s class.

This prevents code repetition for similar objects that need some extra or more specific features.

Subclasses can be created using the extends keyword and super().

For example, we can extend Person to the subclass Student.

class Person {
  constructor(firstName, lastName, birthDay) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.birthDay = new Date(birthDay);
  }

  getFullName() {
    return `${this.firstName} ${this.lastName}`;
  }

  getBYear() {
    return this.birthDay.getFullYear();
  }
}

// Student Subclass

class Student extends Person {
  constructor(firstName, lastName, birthDay, school) {
    super(firstName, lastName, birthDay);
    this.school = school;
  }
}

// Instantiate Student
const stu1 = new Student("John", "Smith", "10-3-2008", "junior high school");

console.log(stu1); // Student {firstName: "John", lastName: "Smith", birthDay: Fri Oct 03 2008, school: "junior high school"}

VII. static method

The static keyword defines a static method for a class.

Static methods are not called on individual instances of the class but are called on the class itself.

 static generatePassword() {
    return Math.floor(Math.random() * 10000);