JS Classes
In this page:
class ClassName {
constructor(parameter) {
this.property = parameter;
}
methodName() {
// body
}
}
const object = new ClassName(argument);
Class Syntax and the Constructor
A class is declared with the class keyword, and its constructor method runs automatically whenever a new instance is created with new, typically used to set up the instance's initial properties.
Example: Class Syntax and the Constructor
// Define the class `User`
// Define the class `User`
class User {
// Constructor taking `name`, runs when a new instance is created
// Constructor taking `name`, runs when a new instance is created
constructor(name) {
// Assign `name` to `this.name`
// Assign `name` to `this.name`
this.name = name;
}
}
// Declare the constant `u` as a new `User` instance
// Declare the constant `u` as a new `User` instance
const u = new User("Sam");
// Print `u.name` to the console
// Print `u.name` to the console
console.log(u.name);
Methods
Methods defined inside a class become available on every instance created from it, letting each object built from the class share the same behavior without duplicating that code.
Example: Methods
// Define the class `User`
// Define the class `User`
class User {
constructor(name) { this.name = name; }
greet() { console.log(`Hi, ${this.name}`); }
}
// Create a new `User` instance with `"Amit").greet(`
// Create a new `User` instance with `"Amit").greet(`
new User("Amit").greet();
Inheritance With extends and super
extends lets one class build on another, inheriting its properties and methods, and super() inside a subclass's constructor calls the parent class's constructor, which must run before this can be used.
Example: Inheritance With extends and super
// Define the class `Animal`
// Define the class `Animal`
class Animal {
constructor(name) { this.name = name; }
}
// Define the class `Dog`, inheriting from `Animal`
// Define the class `Dog`, inheriting from `Animal`
class Dog extends Animal {
// Constructor taking `name`, runs when a new instance is created
// Constructor taking `name`, runs when a new instance is created
constructor(name) {
// Call `super(name)`
// Call `super(name)`
super(name);
}
bark() { console.log(`${this.name} says woof`); }
}
// Create a new `Dog` instance with `"Rex").bark(`
// Create a new `Dog` instance with `"Rex").bark(`
new Dog("Rex").bark();
Static Methods
A static method belongs to the class itself rather than to individual instances, called directly on the class name, and is commonly used for utility functions closely related to the class but not tied to any specific instance's data.
Example: Static Methods
// Define the class `MathUtil`
// Define the class `MathUtil`
class MathUtil {
static double(n) { return n * 2; }
}
// Print `MathUtil.double(5)` to the console
// Print `MathUtil.double(5)` to the console
console.log(MathUtil.double(5));
Getters and Setters
get and set define special methods that run automatically when a property is read or assigned, letting you compute a derived value on read or validate a value before it's stored, while the calling code still uses ordinary property syntax.
Example: Getters and Setters
// Define the class `Circle`
// Define the class `Circle`
class Circle {
constructor(radius) { this._radius = radius; }
get area() { return Math.PI * this._radius ** 2; }
set radius(value) { this._radius = value; }
}
// Declare the constant `c` as a new `Circle` instance
// Declare the constant `c` as a new `Circle` instance
const c = new Circle(2);
// Print `c.area` to the console
// Print `c.area` to the console
console.log(c.area);
- Forgetting to call super() in a subclass's constructor before using this, which throws an error in any class that extends another.
- Trying to call a class directly as a function without new, class functions specifically require the new keyword to create an instance.
- Confusing static methods, called on the class itself, with regular methods, called on instances created from the class.
- A class defines a constructor and methods shared by every object, called an instance, created from it with new.
- extends and super enable inheritance, letting one class build on and reuse another's behavior.
- static methods belong to the class itself, while getters and setters let you control how properties are read and assigned.
Classes have been supported in all major browsers since 2015 and are a fully standard, widely used part of modern JavaScript.
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- JS Dates
- JS Math
- JS Conditionals
- JS Switch
- JS Loop For
- JS Loop While
- JS Iterables
- JS Sets
- JS Maps
- JS typeof
- JS Type Conversion
- JS Destructuring
- JS Arrow Functions
- JS Classes
- JS Modules
- JS Promises
- JS Async/Await
- JS DOM
- JS DOM Methods
- JS Events Advanced
- JS DOM Navigation
- JS DOM Collections
- JS Async Callbacks
- JS Async Parallel
- JS Date Set
- JS Set Logic