Our Tech Journal
  • Introduction
  • Overview
    • Introduction
  • Javascript
    • JS Runtime, Env Context, Scopes, Hoisting & Execution Cycle, var/let/const, this
    • Javascript Runtime Env
      • Event loops
    • What are WebVitals
    • Module Patterns in JS - CommonJS, AMD and ES6 classes
    • Lexical Environment in JS
    • var vs let/const
    • Temporal Dead Zone (TDZ)
    • Execution Context
    • 'this' keyword
    • undefined value in JS
    • Function expressions vs Function statements
    • Javascript Code Execution
    • Closures
    • JS : Object Oriented Programming
    • Prototypal Inheritance
      • Creating Object in Javascript
    • NaN
    • Objects
      • Ways to create
      • ways to check if Object is empty
      • Object's own properties
    • use strict mode
    • typeof vs instanceof
    • Promise
    • localStorage and SessionStorage
    • Event handlers
    • Event bubbling
    • Event Delegation
    • XHR Request
    • Promise Error: Catch vs Error Function in Then
    • Immutability
    • Useful Code samples
    • window, document, screen
    • window.innerheight, outer height, screen.height
    • JS Design Patters
    • Interview Questions
    • Encapsulation - Module Patterns in JS
    • Redesigning services using Entity framework approach
  • JS Functions
    • JS Array - Slice vs Splice
  • PWA
    • What is PWA
  • ES6
    • Sets
    • Maps
    • spread vs destructure operator
  • Web
    • http / https
    • CORS
    • CSRF
    • XSS Attack
    • What is SPA
    • Semantic Elements in HTML
  • Angular 4
    • Angular vs React
    • Change Detection
    • Lazy Loading of modules
    • Preloading
    • AOT Compilation
    • Route Guards
    • Shared Modules
    • Tree Shaking
    • LifeCycle Hooks
    • ngRx
    • Observables
    • Observable vs Subject
      • BehaviorSubject
    • Observables vs Promises
    • Builtin Directives
      • Temp
      • Structural Directives
        • ngForTemp
        • ngSwitch
        • ngFor
        • ngIf
      • Attribute Directives
        • temp
        • ngClass
        • ngStyle
    • Routing
      • Routing in Angular
      • Setting up basic routing
      • routerLink and routerLinkActive
      • router.navigate and ActivatedRoute
      • Route Params as Observables
      • redirectTo
      • relativeTo
      • pathMatch
      • ActivatedRoute
      • Routing in Angular
      • Passing Data with Route
      • Route Parameters
    • Intercept HTTP request
    • Custom Directives
    • Communication between components
    • Angular Modules
    • Reactive Forms
    • Unit Testing
      • TestBed and component fixture
      • Testing HttpClient requests
      • Testing fakeAsync
  • GraphQL
    • Introduction
    • Server
    • Client Side
    • GraphQL in Angular
    • Queries
      • temp
      • query with parameters
      • aliases
      • fragments
      • @include directive
      • @skip directive
      • variables
      • Inline Fragments
  • CSS
    • What is Box model?
    • display: block, inline or inline-block
    • CSS Selector: Combinators
    • CSS Pseudo-classes
    • CSS Pseudo-elements
    • CSS3 Filter
    • CSS3 Transitions
    • Media Queries
    • Flex vs Grid
    • CSS 3 Grids
    • What is Flexbox?
    • position: relative vs absolute
  • SASS
    • Mixins
    • Lists
    • Maps
  • RxJS
    • throttle vs debounceTime
    • distinctUnitChange
    • reduce vs scan
  • Typescript
    • Typeguards
    • Pattern Matching with Typescript
    • TS Decorators
    • Using LINQ in TS
  • NodeJS
    • NodeJS Security Checklist
    • What is Node.js
  • REACT
    • React - VDOM - Under the hood
    • Synthetic events in React
    • Routing - React Router 4
    • React Custom hook
    • Higher-Order Component
    • REDUX
    • Redux Thunk vs Redux Saga
    • forceUpdate()
    • Storing Data in React
    • Error Handling
    • React Context
    • How React-Native works
    • refs
    • Server-side Rendering
    • Jest setup file
    • React-test-renderer
    • Lifecycle
    • React Testing Library
    • React Query
  • JWT
    • What is JWT and How it works
Powered by GitBook
On this page
  • Class-based or Classical Inheritance
  • Prototypal Inheritance
  • Core prototype object
  • Base Prototypes for Array and Functions
  • Everything in javascript, that is not a primitive type, is an object. Arrays, functions are all javascript objects having a prototype.
  • Function objects
  • Array objects:
  1. Javascript

Prototypal Inheritance

PreviousJS : Object Oriented ProgrammingNextCreating Object in Javascript

Last updated 2 years ago

* Most of the things we have learnt about Javascript prototypal inheritance is from the wonderful .

Inheritance = a language feature that allows an object to access the properties and methods of another object.

Class-based or Classical Inheritance

In class-based inheritance, given a ClassA, you can inherit ClassB from ClassA so that ClassB can access all the public/protected properties and methods of ClassA. To inherit, the language would provide keyword like inherits or operator like : .

Prototypal Inheritance

In Javascript, whenever we create an object, lets say Obj1, an internal property called proto gets auto-created for the object. This proto property points to a base object. To this proto object, we can add any property , let's say prop2. We can access this property as Obj1.prop2. Javascript engine would look for prop2 in Obj1, when it won't find the property as direct child of Obj1, it would start looking for it under obj1's proto object. Thus a property under proto object can be accessed directly using dot notation on Obj1.

(in Chrome V8, this proto property of any object is referred as proto: `myObject.proto.proto.....)

The proto object can itself have another proto property that you may use to add more properties and methods, let's say another property prop3 is added to the Obj1.proto.proto. And this property can again be accessed with dot operator on 'Obj1' like Obj1.prop3. Thus it may have a prototypal chain.

Core prototype object

At the bottom of the prototypal chain, there would always be a base object. This is called the base object. Base object is called 'Object', it's at the very bottom of the prototypal chain and have some predefined methods and properties like ToString, hasOwnProperty, isPropertyOf, valueOf etc.

Base Prototypes for Array and Functions

Inheriting from the above core prototype object, javascript engine would also create base prototypes for other non-primitive types viz. Array and Function as below:

Everything in javascript, that is not a primitive type, is an object. Arrays, functions are all javascript objects having a prototype.

Function objects

Functions in javascript are objects. The prototype object for a function is Empty function:

function Empty(){}

This Empty function, which is prototype for all the functions in javascript has some predefined properties and methods like:

arguments: an array of the arguments of the function.
call, bind, apply - functions that allow you to pass an object to which `this` would point to in the function and then list of parameters.
name: name of the function

This Empty function which is prototype for the function would have its prototype set to the base object: Object {}.

Array objects:

Arrays in javascript are objects. The prototype object for arrays is [], an empty array.

this empty array prototype for array objects has properties like length, indexOf, forEach, filter, map, keys, join, pop, push, reduce, reverse etc.

This [] array which is prototype for any array would have its prototype set to the base object: Object {}.

video series by Anthony Alecia