Click here to Skip to main content
13,597,382 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


3 bookmarked
Posted 28 May 2014
Licenced CPOL

Simple Inheritance Example in JavaScript

, 28 May 2014
Rate this:
Please Sign up or sign in to vote.
This is a simple inheritance example in JavaScript


The aim of the tip is to explore a simple inheritance example in JavaScript. Yes, there are quite a good number of options to follow, but this solution worked fine for me.


I have been working with JavaScript for a short period of time. By this time, I have been in a situation where the same code segment was repeated again and again. So I needed a way to reform them to reduce the code and centralize them. One option was to write the JQuery plugin. But using the normal function has not been the best option. I wanted to use inheritance as I do in C#. So I started Googling, after exploring some solutions, I adopted a solution which helped me a lot.

Using the Code

We will start with a basic base object Math which has the ability to find odds and evens from a given number array, using odds and evens prototypes.

/*Base object with prototypes*/
function Math() { }
Math.prototype.odds = function(numberArray) {
    return $.grep(numberArray, function(number) {
        return number % 2 === 1;

Math.prototype.evens = function (numberArray) {
    return $.grep(numberArray, function (number) {
        return number % 2 === 0;

Before we move on to the child object Calculator which will inherit from the base Math object, we need a utility function to help use in inheritance. And it would be like this, which copies all the prototypes of a given object. And returns the new object.

/*Utility to clone objects prototypes*/
function prototypeClone(baseObject) {
    function baseClone() { }
    baseClone.prototype = baseObject.prototype;
    return new baseClone();

Now we are going to use this utility function with the child object Calculator to inherit from the base object Math like this. Reforming the prototype constructor is very important work to do, the last line:

/*Child inheriting from base object*/
function Calculator() { }
Calculator.prototype = prototypeClone(Math);     //copy all the base prototypes to child
Calculator.prototype.constructor = Calculator;   //reform the prototype constructor to Calculator 

After this inheritance, the child Calculator object will be able to use odds, evens and all prototypes, which belong to the base object Math. And we can add new ones too to the Calculator object if we want to. Now test how the Calculator object works.

$(document).ready(function () {
    var calculator = new Calculator();
    var odds = calculator.odds([1, 2, 3, 4, 5]);        
    var evens = calculator.evens([1, 2, 3, 4, 5, 6]);   
    alert(odds);    //1,3,5
    alert(evens);   //2,4,6


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Bangladesh Bangladesh
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralTip... Pin
Nitij6-Jun-14 0:04
professionalNitij6-Jun-14 0:04 
GeneralMy vote of 3 Pin
Murali Gowda28-May-14 19:29
professionalMurali Gowda28-May-14 19:29 
GeneralRe: My vote of 3 Pin
Nikola Breznjak29-May-14 2:28
professionalNikola Breznjak29-May-14 2:28 
GeneralRe: My vote of 3 Pin
Murali Gowda29-May-14 3:13
professionalMurali Gowda29-May-14 3:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.180621.3 | Last Updated 28 May 2014
Article Copyright 2014 by DiponRoy
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid