JS: Prototype and Inheritance
Prototype of a Object (aka Parent Object)
Each Object in JavaScript may have a 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 a parent is null
.
This is the technical meaning of inheritance in JavaScript.
Prototype Chain (aka Parent Chain)
Find a Object's Parent
Create Object with Specified Parent
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
.
d
is a (own or inherited) property of the objecta.b.c
.c
is a (own or inherited) property of the objecta.b
.b
is a (own or inherited) property of the objecta
.
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 Array.prototype.slice.call
:
- The parent of
Array.prototype.slice.call
isFunction.prototype
. - The parent of
Array.prototype.slice
isFunction.prototype
. - The parent of
Array.prototype
isObject.prototype
. - The parent of
Array
isFunction.prototype
.
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
/* the parent of any function is Function.prototype */ console.log( [ Object, Function, Array, Date, RegExp, Set, Map, Symbol, class {}, function () {}, (x) => 3, ].every((x) => (Reflect.getPrototypeOf(x) === Function.prototype)), );
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
console.log( Reflect.getPrototypeOf(Math) === Object.prototype, Reflect.getPrototypeOf(JSON) === Object.prototype, );
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〕
/* Parent of builtin constructor object's prototype property is Object.prototype */ console.log( Reflect.getPrototypeOf(Function.prototype) === Object.prototype, Reflect.getPrototypeOf(Array.prototype) === Object.prototype, Reflect.getPrototypeOf(Date.prototype) === Object.prototype, Reflect.getPrototypeOf(RegExp.prototype) === Object.prototype, );
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.
/* Object.prototype doesn't have any parent */ console.log( Reflect.getPrototypeOf(Object.prototype) === null, );
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
- if it's a function object, its parent is
Function.prototype
. - if it's a object object, its parent is
Object.prototype
. - if it's a array object, its parent is
Array.prototype
. - if it's
new X()
, its parent isX.prototype
. (normally)
/* parent of user created objects */ console.log( 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, );
Function to Show Prototype Chain
JavaScript, Object and Inheritance
- JS: Object Tutorial
- JS: Object Overview
- JS: Object Type
- JS: Test is Object Type 🚀
- JS: Determine Type of Object
- JS: Prototype and Inheritance
- JS: Prototype Chain
- JS: Object.prototype.isPrototypeOf
- JS: Get Set Prototype
- JS: Show Prototype Chain 🚀
- JS: Create Object
- JS: Object Literal Expression
- JS: Create Object with Parent X
- JS: Prevent Adding Property
- JS: Deep Copy Object, Array 🚀
- JS: Test Object Equality 🚀
- JS: Add Method to Prototype
- JS: Object Object
- JS: Object Constructor
- JS: Object.prototype