Skip to main content
← Back to Blog
2026-02-21 · 7 min readWeb DevelopmentUpdated Oct 29, 2026

From Prototypes to ES6 Classes: Understanding JavaScript's Object-Oriented Nature

JavaScript classes look like Java blueprints, but they run on prototypes. This guide builds the same Robot object twice, once with a constructor function and once with a class, rebuilds extends by hand with Object.create, walks the prototype chain step by step, and shows why it matters for lost this in callbacks, polyfills and memory.

Avnish Yadav
AI Automation Engineer & Salesforce Developer
5 views
From Prototypes to ES6 Classes: Understanding JavaScript's Object-Oriented Nature

If you come from Java, C# or C++, JavaScript's object model can feel strange. You see class, new and extends, and you assume classical inheritance: blueprints that stamp out copies. That isn't what happens. This guide is for developers who use JavaScript classes and want to know what runs underneath. You'll build the same object with a constructor function and with a class, rebuild extends by hand, and see why the difference explains lost this, polyfills and shared methods.

This is part 7 of the Frontend & JavaScript Fundamentals series. It follows practical DOM manipulation, where the elements and ui objects were plain objects whose prototype is Object.prototype.

Key takeaways

  • A class is a constructor function at runtime; its methods live once on prototype and every instance links to them.
  • Property lookup walks the prototype chain from the object up to null; extends just links two chains.
  • this depends on how a function is called, so a method passed as a callback loses its object unless you bind it.

Delegation, not blueprints

Prototypal inheritance is JavaScript's model where an object links to another object, its prototype, and borrows any property it doesn't have itself. In Java, a class is a blueprint, and each instance gets its own copy of the structure. In JavaScript, objects don't copy behaviour. They link to it.

MDN describes the lookup: when you read a property, the engine checks the object, then its prototype, then that prototype's prototype, until it finds a match or reaches the end of the chain. This is often called behaviour delegation.

MDN's guide is direct about classes: "Classes are syntax sugar over constructor functions", and "even the 'classes' as syntax constructs are just constructor functions at runtime." They add some rules of their own, which the table below lists.

1. Constructor functions and prototypes

Before ES2015 (ES6), you built repeatable objects with a constructor function. It's the clearest way to see where methods live.

// The constructor function
function Robot(name, type) {
    this.name = name;
    this.type = type;
    // Note: methods are NOT defined in here.
}

// Add the method to the prototype
Robot.prototype.greet = function() {
    return `I am ${this.name}, a unit of type ${this.type}.`;
};

const unit1 = new Robot("RX-78", "Gundam");
const unit2 = new Robot("Eva-01", "Evangelion");

console.log(unit1.greet()); // I am RX-78, a unit of type Gundam.
console.log(unit1.greet === unit2.greet); // true
console.log(Object.getPrototypeOf(unit1) === Robot.prototype); // true

If you wrote this.greet = function () {…} inside Robot, every new robot would get its own copy of the function. On the prototype, there's one function, and both robots link to it. That's why unit1.greet === unit2.greet is true.

Use Object.getPrototypeOf() to see the link. MDN marks the obj.__proto__ accessor as deprecated and recommends Object.getPrototypeOf() and Object.setPrototypeOf() instead.

2. The same object as a class

Now write it with the class keyword and check what you get.

class RobotClass {
    constructor(name, type) {
        this.name = name;
        this.type = type;
    }

    greet() {
        return `I am ${this.name}, a unit of type ${this.type}.`;
    }
}

const unit3 = new RobotClass("Wall-E", "Compactor");

console.log(typeof RobotClass); // "function"
console.log(Object.hasOwn(RobotClass.prototype, "greet")); // true
console.log(unit3.greet()); // I am Wall-E, a unit of type Compactor.

typeof says "function", and greet sits on RobotClass.prototype, exactly where you put it by hand in section 1. The class syntax sets up the prototype for you. It isn't identical, though. MDN's Classes reference lists the differences:

Feature Constructor function Class
Where methods go You assign them to prototype Put on prototype automatically
Call without new Runs with the wrong this (the global object, or a TypeError in strict mode) Throws a TypeError
Strict mode Only if you opt in Always, for the whole body
Hoisting Usable before the declaration Temporal dead zone, like let
Private state Closures only #private fields
When to use Reading older code and libraries New code

3. Inheritance and the prototype chain

Inheritance is where the two styles look most different. Make an AttackRobot that adds a weapon.

With extends

class AttackRobot extends RobotClass {
    constructor(name, type, weapon) {
        super(name, type); // runs the parent constructor
        this.weapon = weapon;
    }

    attack() {
        return `${this.name} attacks with ${this.weapon}!`;
    }
}

const striker = new AttackRobot("Zaku", "Mobile Suit", "a heat hawk");
console.log(striker.greet()); // I am Zaku, a unit of type Mobile Suit.
console.log(striker.attack()); // Zaku attacks with a heat hawk!

extends links the prototypes, and super() runs the parent's setup. MDN notes that a subclass constructor must call super() before it uses this.

By hand, with prototypes

Here is roughly what extends sets up, written against the Robot constructor from section 1. Compilers such as Babel do the same work with helper functions.

function AttackRobotProto(name, type, weapon) {
    // 1. Run the parent constructor with this object as `this`
    Robot.call(this, name, type);
    this.weapon = weapon;
}

// 2. Link the prototypes: a new object that delegates to Robot.prototype
AttackRobotProto.prototype = Object.create(Robot.prototype);

// 3. Fix the constructor reference (otherwise it points to Robot)
AttackRobotProto.prototype.constructor = AttackRobotProto;

// 4. Add the subclass method
AttackRobotProto.prototype.attack = function() {
    return `${this.name} attacks with ${this.weapon}!`;
};

// 5. Link the constructors too, so static methods are inherited
Object.setPrototypeOf(AttackRobotProto, Robot);

const gunner = new AttackRobotProto("GM", "Mobile Suit", "a beam spray gun");
console.log(gunner.greet()); // I am GM, a unit of type Mobile Suit.
console.log(gunner instanceof Robot); // true

MDN describes Object.create() as making a new object with an existing object as its prototype. That one call builds the link between the two chains. Step 5 matches MDN's extends page, which says extends sets the prototype of both the child class and its prototype object.

4. Walking the chain

When you call gunner.greet(), the lookup goes like this:

  1. The instance: does gunner have its own greet? No. It has name, type and weapon.
  2. Its prototype: does AttackRobotProto.prototype have greet? No, only attack and constructor.
  3. The next prototype: does Robot.prototype have greet? Yes. It runs, with this set to gunner.

You can print the chain yourself:

let link = Object.getPrototypeOf(gunner);
while (link) {
    console.log(Object.getOwnPropertyNames(link));
    link = Object.getPrototypeOf(link);
}
// [ 'constructor', 'attack' ]
// [ 'constructor', 'greet' ]
// [ 'constructor', '__defineGetter__', ... ] (Object.prototype)

If the lookup reaches the end of the chain, the property is undefined. Calling it is a different story: gunner.fly() throws TypeError: gunner.fly is not a function, because you can't call undefined.

Fixed in this version

The earlier version said a missing method "would return undefined". Reading a missing property gives undefined, but calling a missing method throws a TypeError. It also pointed readers to __proto__, which MDN marks as deprecated.

5. Why prototypes matter in everyday code

Lost this in callbacks

MDN's this reference explains that this is set by how a function is called, not where it's defined. A method is just a function on the prototype. Pass it somewhere on its own and it loses its object. Class bodies run in strict mode, so this becomes undefined and the method throws.

class Counter {
    count = 0;
    increment() { this.count += 1; return this.count; }
}

const counter = new Counter();
const loose = counter.increment;
try {
    loose(); // `this` is undefined here
} catch (error) {
    console.log(error.constructor.name); // TypeError
}

// Fix: bind the method to its object
const bound = counter.increment.bind(counter);
console.log(bound()); // 1

An arrow-function class field (increment = () => {…}) also fixes this. MDN's this page notes the trade-off: "each instance of the class will have its own copy of the method, which increases memory usage."

Polyfills

MDN defines a polyfill as "a piece of code (usually JavaScript on the Web) used to provide modern functionality on older browsers that do not natively support it." Because prototypes are ordinary objects, a polyfill can add a missing method to Array.prototype, and every array gets it. MDN's prototype guide says that backporting newer features is the only good reason to extend a built-in prototype. Adding your own methods there risks clashing with a future version of the language.

// Avoid: your own helper on a built-in prototype
// Array.prototype.last = function () { return this[this.length - 1]; };

// Better: a plain function
const last = (items) => items[items.length - 1];
console.log(last([1, 2, 3])); // 3

For real polyfills, use a maintained library such as core-js rather than writing your own.

Shared methods and memory

Instance properties live on each object; prototype methods are shared. As a rule of thumb, this matters most when you create many objects, such as entities in a game or points in a chart. Keep methods on the prototype (the default with class) unless you need a bound copy per instance.

Classes on top, prototypes underneath

Classes are a good addition to the language. They give you strict mode, a clear syntax for inheritance and private fields. But they sit on the prototype chain, not in place of it. When something behaves oddly, think of objects as collections of properties with a link to another object, and follow the link.

Next in the series, OOP principles and design patterns in JavaScript uses these classes to build modules, singletons and observers.

Frequently asked questions

Are JavaScript classes just syntactic sugar?

Mostly. MDN calls them syntax sugar over constructor functions, and typeof a class is "function". They also add rules a constructor function lacks: they must be called with new, their bodies run in strict mode, and they support #private fields.

What is the difference between prototype and proto?

prototype is a property of constructor functions and classes; it's the object new instances will link to. __proto__ is an old accessor for an object's own link. MDN marks __proto__ as deprecated and recommends Object.getPrototypeOf() instead.

Why does this become undefined in my class method?

You called the method without its object, for example by passing counter.increment as a callback. this is set at call time, and class code runs in strict mode, so it's undefined. Use bind() or an arrow-function class field.

Should I still use constructor functions?

For new code, use classes; MDN's Object.create() page says class syntax should be preferred in modern code. You'll still meet constructor functions in older libraries, so it helps to read them.

Sources

Verified against the sources below on October 29, 2026. Products and docs change often: check the linked sources if something looks different.

  1. MDN: Inheritance and the prototype chain
  2. MDN: Classes
  3. MDN: extends
  4. MDN: Object.create()
  5. MDN: Object.prototype.__proto__ (deprecated)
  6. MDN: this
  7. MDN: Polyfill (glossary)
  8. core-js README
All 30 parts in this series
  1. Part 01Overcoming the Daunting Start: A Strategic Roadmap for New Frontend Developers
  2. Part 02Beyond the Tags: Mastering HTML Architecture for Product-Based Roles
  3. Part 03From Selectors to BEM: CSS Concepts You Must Know for Professional Projects
  4. Part 04Mastering the Primitives: A Builder's Guide to JavaScript Data Types and Functions
  5. Part 05Demystifying Scope and Closure: Key Concepts for Writing Robust JavaScript
  6. Part 06Beyond Frameworks: Practical DOM Manipulation Techniques for High-Performance UIs
  7. Part 07From Prototypes to ES6 Classes: Understanding JavaScript's Object-Oriented Nature
  8. Part 08Architecting Scalable JavaScript: OOP Principles & Design Patterns
  9. Part 09Mastering the Metal: Essential Web APIs & Browser Internals for Frontend Engineers
  10. Part 10Understanding the JavaScript Runtime: Event Loop, Call Stack, and Task Queue Explained
  11. Part 11Mastering Asynchronous Operations with JavaScript Promises: A Builder's Guide
  12. Part 12Modern Async Patterns: Making Asynchronous Code Readable with Async/Await and Generators
  13. Part 13Inside the V8 Engine: A Deep Dive into JavaScript Execution
  14. Part 14Demystifying CORS, HTTP, and Security: A Builder’s Guide
  15. Part 15REST vs. GraphQL: A Frontend Engineer’s Reality Check
  16. Part 16Secure Frontend Auth: The Definitive Guide to JWTs, Cookies, and Storage Strategies
  17. Part 17Why TypeScript Matters: Adding Type Safety to Your JavaScript Projects
  18. Part 18Getting Started with React: Deconstructing JSX and Component Architecture
  19. Part 19React Architecture: Mastering State and Props in Class vs. Functional Components
  20. Part 20Mastering React Hooks: From useState to Building a Production-Ready Custom Hook
  21. Part 21React State Management: Context API vs. Redux & Building a Theme Switcher
  22. Part 22React Architecture: Advanced Navigation and Data Flow Patterns
  23. Part 23Automated Testing in React: Unit Testing with Jest and React Testing Library
  24. Part 24Mastering Chrome DevTools: Debugging Techniques That Save Hours of Development Time
  25. Part 25Automating Code Quality: The Modern Stack with ESLint, Prettier, and Husky
  26. Part 26Demystifying the Build Pipeline: A Deep Dive into Webpack and Babel Configuration
  27. Part 27Improving Performance with Server-Side Rendering: Concepts and Implementation
  28. Part 28From Development to Production: A Builder's Guide to Frontend Deployment Strategies
  29. Part 29Introduction to Micro Frontends: Building Scalable Architectures
  30. Part 30The Frontend Architecture: From Junior Developer to Tech Lead
Written by

Avnish Yadav

AI Automation Engineer & Salesforce Developer

Salesforce developer since June 2018. I build AI agents, n8n workflows and Salesforce integrations, and write up what holds up in real use.

About meLinkedIn

Share
Discussion

Comments

Loading comments...

Add a comment

Comments are reviewed before they appear.