JS: Prototype and Inheritance

By Xah Lee. Date: . Last updated: .

Prototype of a object

Each Object in JavaScript may have one parent object called its prototype object. When this parent is the value null, we say that it doesn't have a parent.

Inheritance

When a property is looked up (e.g. 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 no more parent.

Prototype chain

Find a object's parent

Create object with specified parent

Dot notation a.b.c does not show prototype chain

Prototype tree

Function to show prototype chain