TechTalks
Chapter 5 of 12

5.1 Prototype & Inheritance Architecture

JavaScript does not use traditional class-based inheritance under the hood. Instead, it relies on Prototypal Inheritance via a chain of prototype linkages ([[Prototype]] or __proto__).

Prototype Chain Mechanicsjavascript
function Animal(name) {
  this.name = name;
}
Animal.prototype.eat = function() {
  return `${this.name} is eating.`;
};

const dog = new Animal('Buddy');
console.log(dog.eat()); // 'Buddy is eating.'
console.log(dog.__proto__ === Animal.prototype); // true
console.log(Animal.prototype.__proto__ === Object.prototype); // true

5.2 Modern ES6+ Classes Syntax

ES6 introduced the class syntax as syntactical sugar over JavaScript's prototype system. Modern JS adds true private instance fields and static blocks.

Modern Class with Private Fieldsjavascript
class BankAccount {
  // Private field (Hard privacy in engine)
  #balance = 0;

  constructor(owner, initialDeposit) {
    this.owner = owner;
    this.#balance = initialDeposit;
  }

  // Getter
  get balance() {
    return this.#balance;
  }

  deposit(amount) {
    if (amount <= 0) throw new Error('Deposit must be positive');
    this.#balance += amount;
    return this.#balance;
  }
}

const account = new BankAccount('Alice', 500);
account.deposit(200);
console.log(account.balance); // 700
// console.log(account.#balance); // SyntaxError: Private field '#balance' must be declared in an enclosing class