Classes are templates for creating objects. They combine data with code that operates on that data. JavaScript
classes are built on prototypes but introduce their own syntax and semantics. Just like functions, classes can be
defined in two ways: with a class expression or a class declaration. Class expressions can be anonymous or named
(different from the variable they are assigned to). Unlike function declarations, class declarations have the same
"temporal dead zone" constraints as variables declared with let or const, so they are not
"hoisted".
A class is a special kind of function, but instead of the keyword function we use the keyword
class. Properties are typically initialized inside the constructor() method.
class ClassName {
constructor() { /* ... */ }
}
class Person {
constructor(name, surname) {
this.name = name;
this.surname = surname;
}
}
The example above creates a class named ClassName/Person that has initial properties
name and surname. A class (ang. Class) is not an object — it is a blueprint for JavaScript
objects. Key points: define a class with class, initial state in constructor(),
inheritance with extends and superclass call with super(). Static methods
(static) are called on the class, not on an instance.
Classes in JavaScript are templates for creating objects. They are defined with the keyword class,
and the initial state is set in constructor(). Unlike functions, class declarations are not hoisted, so
they must be defined before use.
Classes support inheritance with extends and calling the superclass constructor with
super(). Methods are by default on the prototype (instance), while static methods (static)
are on the class itself. Getters and setters allow controlled access to properties.
The table below shows all key concepts: definition, constructor, methods, inheritance, static members, get/set and private properties. For each element, the description, syntax and concrete code example are provided.
| Name | Description | Syntax | Example |
|---|---|---|---|
| Class Declaration | Create a class with the keyword class (not hoisted). | class Name { ... } |
class Person { constructor(name) { this.name = name; } } |
| Class Expression | Class stored in a variable (can be anonymous). | const Name = class { ... }; |
const Person = class { constructor(name) { this.name = name; } }; |
| constructor() | Initializes a new instance (called on new). |
constructor(parameters) { ... } |
constructor(name, surname) { this.name = name; this.surname = surname; } |
| new | Create a new instance of the class. | const instance = new Class(args); |
const ana = new Person("Ana", "Novak"); |
| Instance Method | Method that every instance can call. | methodName() { ... } |
fullName() { return this.name + " " + this.surname; } |
| static | Static method on the class (not on the instance). | static methodName() { ... } |
static fromString(str) { return new Person(str); } |
| get | Getter – access property as a value. | get name() { return ...; } |
get age() { return this._age; } |
| set | Setter – set property with validation. | set name(value) { ... } |
set age(v) { if (v > 0) this._age = v; } |
| extends | Create a subclass that inherits properties and methods from the superclass. | class SubClass extends SuperClass { ... } |
class Student extends Person { constructor(name, year) { super(name); this.year = year; } } |
| super() | Call the superclass constructor (mandatory in subclass before using this). |
super(arguments); |
super(name, surname); |
| super.method() | Call a superclass method from a subclass. | super.method(arguments); |
super.fullName(); |
| instanceof | Check if an object is an instance of a specific class. | obj instanceof Class |
ana instanceof Person; // true |
| # (Private Property) | Property not accessible outside the class (ES2022+). | #property; |
#age; constructor(a) { this.#age = a; } |
| # (Private Method) | Method not accessible outside the class (ES2022+). | #method() { ... } |
#calculate() { return this.#age * 2; } |
| static property | Property on the class itself (not on the instance). | static property = value; |
static type = "human"; |
| Override Method | Subclass overrides a superclass method with the same name. | methodName() { ... } |
fullName() { return "Student: " + super.fullName(); } |
| constructor (property) | Returns the function that created the instance. | obj.constructor |
ana.constructor; // class Person { ... } |
| prototype | Object containing all instance methods. | Class.prototype |
Person.prototype.fullName; |
| name | Returns the class name as a string. | Class.name |
Person.name; // "Person" |