Author Archives: Ebraam Tharwat

Ebraam Tharwat

In this article i 'll share with you some techniques that can be used to implement inheritance in JavaScript. JavaScript is a prototype-based language which contains no class statement, such as is found in C# or Java (confusing, right!).  Instead, JavaScript uses functions as classes (no "classes", just objects).

JavaScript Inheritance techniques:

Pseudo-classical inheritance:

  1. Define the base object with it's properties normally.
  2. Define the derived object  then change it's prototype to be an object of type base object (e.g. derivedObj.prototype = new baseObj();).
  3. Using the derived object prototype, we can add new functions/properties or even modify base object functions with a new implementaion.

Prototypal Inheritance:

  1. Make a helper function create which simply takes an object as a parameter and returns an empty new object that inherits from the object we passed to it.
  2. Then we can modify base object functions implementation or add a new functions/properties.

Parasitic Inheritance:

  1. Define the base object with it's properties normally.
  2. Inside the derived object function/constructor create a base object instance. That object is then 'll be enhanced or augmented with the new properties of the derived object.
  3. Return the base object instance we just created.