Fundamentals 22 min read

Understanding JavaScript Constructors, Prototypes, Classes, and Inheritance

The article thoroughly explains JavaScript constructors, prototype chains, the this binding, and ES6 class syntax, showing how functions become constructors with new, how prototypes link objects, custom new/instanceof implementations, and various inheritance patterns—including parasitic combination inheritance realized by class extends and super.

Sohu Tech Products
Sohu Tech Products
Sohu Tech Products
Understanding JavaScript Constructors, Prototypes, Classes, and Inheritance

This article provides a comprehensive tutorial on core JavaScript concepts such as constructors, prototypes, the this binding, and class syntax. It explains how constructors are just regular functions that can be invoked with new , how the prototype chain works, and why this points to different objects in various call contexts (direct call, method call, call/apply/bind , and arrow functions).

Key points include:

All functions can be used as constructors when called with new , except arrow functions.

The prototype of a constructor ( Constructor.prototype ) becomes the __proto__ of objects created by new Constructor() .

Property lookup follows the prototype chain until Object.prototype and then returns undefined if not found.

Modifying prototype properties affects all instances, while modifying instance properties overrides the prototype value.

The article also demonstrates how to implement a custom new function ( myNew ) and a custom instanceof function ( myInstanceof ) using prototype checks.

It then introduces ES6 class syntax as syntactic sugar over the constructor‑prototype pattern, covering instance fields, prototype methods, static members, and the constructor method. The class example is shown alongside its equivalent constructor‑prototype implementation.

Inheritance patterns are explored in depth:

Prototype chain inheritance – setting Child.prototype = new Parent() (issues: shared reference properties, no constructor arguments).

Constructor inheritance – calling Parent.call(this, args) inside the child constructor (issues: no prototype inheritance).

Combination inheritance – combines both approaches but calls the parent constructor twice.

Parasitic combination inheritance – uses Object.create(Parent.prototype) to avoid the double call.

Finally, the article shows how ES6 class with extends implements parasitic combination inheritance under the hood, using Object.create for the prototype chain and super() to invoke the parent constructor. It also covers static inheritance via super in static methods.

Throughout the tutorial, code snippets are provided to illustrate each concept, and diagrams (referenced as images) visualize the prototype chain and inheritance structures.

JavaScriptobject-orientedPrototypeES6classconstructorinheritance
Sohu Tech Products
Written by

Sohu Tech Products

A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.