JavaScript Notes
Master JavaScript ES6 classes with constructors, methods, static members, inheritance, private fields, and real-world examples. beginner-friendly JS OOP tutorial.
JavaScript classes (introduced in ES6/ES2015) are a clean, readable syntax for creating objects and managing inheritance. Under the hood, classes still use JavaScript's prototype system — they are "syntactic sugar" — but they add powerful features like private fields, static members, and strict constructor rules.
📊 Class Hierarchy Diagram
💻 Example 1 — Basic Class
class Person {
constructor(name, age) {
this.name = name; // instance property
this.age = age;
}
greet() {
return `Hi, I am ${this.name} and I am ${this.age} years old.`;
}
isAdult() {
return this.age >= 18;
}
}
const riya = new Person("Riya", 22);
const aman = new Person("Aman", 16);
console.log(riya.greet());
console.log(aman.greet());
console.log(riya.isAdult());
console.log(aman.isAdult());Hi, I am Riya and I am 22 years old. Hi, I am Aman and I am 16 years old. true false
💻 Example 2 — Class Inheritance with extends and super
class Animal {
constructor(name) {
this.name = name;
}
speak() {
return `${this.name} makes a sound.`;
}
}
class Dog extends Animal {
constructor(name, breed) {
super(name); // call parent constructor
this.breed = breed;
}
speak() { // override parent method
return `${this.name} barks! 🐕`;
}
info() {
return `${super.speak()} Breed: ${this.breed}`;
}
}
const dog = new Dog("Bruno", "Labrador");
console.log(dog.speak());
console.log(dog.info());
console.log(dog instanceof Dog);
console.log(dog instanceof Animal);Bruno barks! 🐕 Bruno makes a sound. Breed: Labrador true true
💻 Example 3 — Static Methods and Properties
class MathHelper {
static PI = 3.14159; // static property — belongs to the class
static circleArea(radius) {
return MathHelper.PI * radius * radius;
}
static add(a, b) {
return a + b;
}
}
// Call on the CLASS, not an instance
console.log(MathHelper.PI);
console.log(MathHelper.circleArea(5).toFixed(2));
console.log(MathHelper.add(10, 20));
// Cannot call on instance
const m = new MathHelper();
// m.circleArea(5) → TypeError: m.circleArea is not a function3.14159 78.54 30
💻 Example 4 — Getters, Setters, and Private Fields
B 95 A
📋 Class Features Quick Reference
| Feature | Syntax | Description |
|---|---|---|
| Constructor | constructor() {} | Runs when new is called |
| Instance method | greet() {} | Available on every instance |
| Static method | static help() {} | Called on the class itself |
| Static property | static count = 0 | Shared across all instances |
| Private field | #secret | Truly private, no outside access |
| Private method | #helper() {} | Internal method, not exposed |
| Getter | get name() {} | Read like a property |
| Setter | set name(v) {} | Write with validation |
| Inheritance | extends ParentClass | Inherit all non-private members |
| Super call | super(args) | Call parent constructor/method |
⚠️ Common Mistakes
❌ Mistake 1 — Forgetting super() in Subclass Constructor
class Vehicle {
constructor(brand) { this.brand = brand; }
}
class Car extends Vehicle {
constructor(brand, model) {
// super(brand); ← MISSING!
this.model = model; // ReferenceError: Must call super before 'this'
}
}ReferenceError: Must call super constructor in derived class before accessing 'this'
❌ Mistake 2 — Classes Are NOT Hoisted
const obj = new MyClass(); // ReferenceError — class not yet defined
class MyClass { }ReferenceError: Cannot access 'MyClass' before initialization
❌ Mistake 3 — Arrow Function Methods Lose Correct this
❌ Mistake 4 — Calling Static Method on Instance
🎯 Key Takeaways
- A class is a blueprint; an instance (via
new) is the real object constructor()runs automatically whennew ClassName()is calledextends+super()enable clean inheritance hierarchies- Static members belong to the class, not instances — use
ClassName.method() - Private fields (
#) are truly private — not accessible outside the class - Getters and setters let you add logic to property reads and writes
- Classes are not hoisted — always define before use
- Under the hood, classes still use JavaScript's prototype chain
❓ Interview Questions
Q1. What is a JavaScript class? > A class is syntactic sugar over JavaScript's constructor functions and prototypes. It provides a clean, readable syntax to create objects with shared methods and support inheritance using extends and super.
Q2. What is the difference between a class and a constructor function? > Both create objects with shared prototypes. Classes enforce new (calling without it throws), do not hoist, always run in strict mode, and support private fields and static blocks. Constructor functions are older and more permissive.
Q3. What does super() do? > super() calls the parent class's constructor. It must be called before accessing this in a subclass constructor. super.method() calls a parent method from within an overriding method.
Q4. What is the difference between a static method and an instance method? > Static methods are called on the class itself (MyClass.method()) and do not have access to instance data. Instance methods are called on objects (obj.method()) and have access to this.
Q5. Are JavaScript classes true classes like in Java? > No. JavaScript classes are prototype-based under the hood. class is syntactic sugar. There is no true class-based instantiation — every "class" is actually a function, and every instance's methods are stored on a shared prototype object.
Q6. What are private class fields and when were they introduced? > Private class fields (prefixed with #) were introduced in ES2022. They are truly private — not accessible outside the class, not enumerable, and not visible via Object.keys() or JSON.stringify().
Q7. Can a JavaScript class extend multiple classes? > No — JavaScript does not support multiple inheritance. A class can only extend one parent class. You can simulate mixins using function composition or mixin patterns.
Q8. What is a getter and setter in a class? > Getters (get prop()) are methods that look like properties when read. Setters (set prop(v)) are methods that look like assignments when written. They allow adding validation or computation without changing the caller's syntax.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for JavaScript Classes Tutorial – ES6 Class Syntax, Inheritance & More.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this JavaScript Master Course topic.
Search Terms
javascript-complete-guide, javascript master course, javascript, complete, guide, oop, classes, javascript classes tutorial – es6 class syntax, inheritance & more
Related JavaScript Master Course Topics