JavaScript: Prototype and Inheritance

By Xah Lee. Date: . Last updated: .

Prototype of a Object

Each object in JavaScript may have one and only one prototype object (aka parent object).

Here's the tech detail how parent object are implemented:

Every object X has a Internal Slot named “prototype”. In JS spec, it's written as “[[Prototype]]”. The value of the [[Prototype]] is some object P, or null. The object P is called the “prototype object” of X. We'll call it “parent object”.


When a property is looked up (for example, x.color), JavaScript looks at the object to see if it has that property, if not, it looks up its parent, and repeat, until a property is found or a parent is null. This is the technical meaning of inheritance in JavaScript.

Prototype Chain

A object's parent of parent of parent etc forms a chain. This is called a prototype chain.

Note: the prototype chain is never circular, and is impossible to create circular prototype chain.

Note: not all objects have a parent, but buildin objects do (except Object.prototype, which is the root, having parent of null).

You can easily creat a object without parent by Object.create(null). [see Object.create]

Find a Object's Parent

Get/Set Prototype

Create Object with Specified Parent

Create Object with Parent X

Dot Notation a.b.c Does Not Show Prototype Chain

When you see a.b.c.d in JavaScript, it has very different meaning than Java or Python, Ruby. This is a major point of confusion.

In Java, in a.b.c.p, the dot means parent/child except the last dot, is object/property. So, a is parent of b, b is parent of c, and c is the object that has member p.

In JavaScript, dot always means a relationship between a object and a property key name. There's no direct syntax to show parent chain.

in JavaScript, when you see a.b.c.d, the left/right thing of any dot has a object/property (own or inherited) relationship.

It's parsed like this: (((a).b).c).d.

The value of the property, such as a.b, could be any object, having nothing to do with any of the object a or a.b or a.b.c.

For example, the expression

Prototype of Standard Objects

Here is a short diagram of JavaScript Prototype Hierarchy.

• Object.prototype
  • {}
  • Function.prototype
    • Object
    • Function
    • Array
    • Date
    • RegExp
    • Set
    • Map
    • Symbol
    • function … {}class … {}
  • Array.prototype
    • []
  • Date.prototype
    • new Date()
  • RegExp.prototype
    • //
  • Set.prototype
    • new Set()
  • Map.prototype
    • new Map()
  • Symbol.prototype
    • Symbol()
  • Math
  • JSON

Those colored like this are user defined.

Following is explanation.

Parent of Functions

Object, Function, Array, Date, RegExp, Set, Map, Symbol, are all functions.

User created function, including arrow function, and user created class, are also functions.

All their parent is Function.prototype

 class {},
 function () {},
 (x =>3)
 .every ( (x => (Reflect.getPrototypeOf(x) === Function.prototype )) )
); // true

[see Array.prototype.every]

Parent of Math JSON

Math, JSON are named objects, whose purpose is as namespace to hold useful methods related to math and JSON. Their parent is Object.prototype

[see Math] [see JSON]

 [ Math, JSON ] .every ( (x => (Reflect.getPrototypeOf(x) === Object.prototype )) )
); // true

Parent of Dot Prototype Objects

These objects: {Function.prototype, Array.prototype, Date.prototype, RegExp.prototype} serve as parents of user created objects of particular sub-type. Their parent is Object.prototype.

[see Determine Type of Object]

] .every ( (x => (Reflect.getPrototypeOf(x) === Object.prototype )) )
); // true

Each of the Function Array Date RegExp function object has a property key "prototype". [see Property Key "prototype"]

For example, you can eval the expression Array.prototype. That means, accessing a property key "prototype" from the object Array. The value of Array.prototype, is a object. There is no special syntax to express this object other than accessing property syntax such as Array.prototype.

Root Prototype of All Objects

The root of all JavaScript standard objects is Object.prototype. Itself doesn't have any parent.

 Reflect.getPrototypeOf(Object.prototype) === null
); // true

Parent of User Created Objects

The parent of user created objects is the value of the constructor function's "prototype" property, by default.

For example

// parent of user created objects
Reflect.getPrototypeOf({}) === Object.prototype &&
Reflect.getPrototypeOf([]) === Array.prototype &&
Reflect.getPrototypeOf(/./) === RegExp.prototype &&
Reflect.getPrototypeOf(function(){}) === Function.prototype &&
Reflect.getPrototypeOf(new Date()) === Date.prototype &&
Reflect.getPrototypeOf(new Map()) === Map.prototype
); // true

Function to Show Prototype Chain

Show Prototype Chain

JavaScript Object and Inheritance

JavaScript in Depth

JavaScript in Depth

Basic Syntax

Value Types





Object and Inheritance



Iterable 🌟