Skip to main content

Class

Definition

The class keyword is used to declare a class in JavaScript. Classes are syntactic sugar over JavaScript's existing prototype-based inheritance, providing a cleaner way to create objects and handle inheritance.

class MyClass {}
typeof MyClass // "function" (classes are functions)

Class declaration

class Person {
constructor(name) {
this.name = name
}

greet() {
return `Hello, I'm ${this.name}`
}
}

const person = new Person('Alice')
person.greet() // "Hello, I'm Alice"

Class components

Constructor

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

Methods

class Person {
constructor(name) {
this.name = name
}

greet() {
return `Hello, I'm ${this.name}`
}

getAge() {
return this.age
}
}

Static Methods

class Math {
static add(a, b) {
return a + b
}
}

Math.add(1, 2) // 3

Getters and Setters

class Person {
constructor(name) {
this._name = name
}

get name() {
return this._name
}

set name(value) {
this._name = value
}
}

Inheritance

extends

class Animal {
constructor(name) {
this.name = name
}

speak() {
return 'Some sound'
}
}

class Dog extends Animal {
speak() {
return 'Woof!'
}
}

const dog = new Dog('Buddy')
dog.speak() // "Woof!"

super

class Animal {
constructor(name) {
this.name = name
}
}

class Dog extends Animal {
constructor(name, breed) {
super(name) // Call parent constructor
this.breed = breed
}
}

Private fields (es2022)

class Person {
#privateField = 'secret'

getPrivate() {
return this.#privateField
}
}

const person = new Person()
person.#privateField // SyntaxError
person.getPrivate() // "secret"

Class expressions

const Person = class {
constructor(name) {
this.name = name
}
}

Best practices

  1. Use classes for OOP: When you need object-oriented patterns.

  2. Understand prototype chain: Classes are syntactic sugar over prototypes.

  3. Use extends for inheritance: Cleaner than manual prototype manipulation.

  4. Call super() in derived constructors: Required before using this.

  5. Consider composition over inheritance: Sometimes composition is better than deep inheritance chains.