Praveen Singh

Simple Inheritance in Javascript

Introduction


Simple prototypes based Inheritance in Javascript

JavaScript is an object-oriented language based on prototypes, rather than being class-based. Because of this different basis, it can be less apparent how JavaScript allows you to create hierarchies of objects and to have inheritance of properties and their values

I will try to explain Inheritance by a simple code example in this article

But before i can start, we have to take a usecase, an example of inheritance to begin with

Class Digram for the example

step by step video tutorial


Code



/*
 * Lets define all the Objects(Classes for Java and C++ guys, mind it, there is nothing call Class in Java)
 */
 function Developer(name) {
    this.name = name;
    this.skill = "no skill";

    this.printSkill = function() {

        console.log("Skill: ", this.skill);

    };

    this.printName = function() {

        console.log("I am : ", this.name);

    };

    this.workOn = function() {

        console.log("Not sure!");

    };

}


function JavaDeveloper(name) {
    this.skill = "Java";
    this.name = name;

    this.workOn = function() {

        console.log("I work on Server side");

    };

}



function JavaScriptDeveloper(name) {
    this.skill = "Javascript";
    this.name = name;

    this.workOn = function() {

        console.log("I work on client side");

    };

}


function NodeJSDeveloper(name) {
    this.name = name;

    this.workOn = function() {
        console.log("I work on server side");

    };
}

/*
 * Inheritance Linking go below
 */
JavaDeveloper.prototype = new Developer();
JavaScriptDeveloper.prototype = new Developer();
NodeJSDeveloper.prototype = new JavaScriptDeveloper();



var developer = new NodeJSDeveloper("praveen");

developer.printSkill();
developer.printName();
developer.workOn();
console.log(developer instanceof Developer);
console.log(developer instanceof NodeJSDeveloper);
console.log(developer instanceof JavaDeveloper);

Summary


Javascript prototypes based Inheritance make it powerful and dynamic, but it is not simple to understand and implement like Java or C++

Here language like DOJO jump in and provide inheritance in Java in plain Javascript

Contact Me


If you want to contact me for feedback or suggestion or doubt. You can do so by commenting on Blogger article or in Youtube comments section.

You can also approach me dropping me mail directly at mails.icodingclub@gmail.com

2 comments:

  1. clear cutttt explanation.. thanks it helps for java lover like me

    ReplyDelete
  2. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Front end developer learn from Javascript Training in Chennai . or learn thru JavaScript Online Training India. Nowadays JavaScript has tons of job opportunities on various vertical industry. JavaScript Training in Chennai

    ReplyDelete