How to Implement Inheritance in Javascript
If you have done Object Oriented Programming in JavaScript, you will know that you can create a class as follows:
Person = function(id, name, age){ this.id = id; this.name = name; this.age = age; alert('A new person has been accepted'); }
So far our class person only has two properties and we are going to give it some methods. A clean way of doing this is
to use its 'prototype' object.
Starting from JavaScript 1.1, the prototype object was introduced in JavaScript. This is a built in object that
simplifies the process of adding custom properties and methods to all instances of an object.
Let's add 2 methods to our class using its 'prototype' object as follows:
Person.prototype = { /** wake person up */ wake_up: function() { alert('I am awake'); }, /** retrieve person's age */ get_age: function() { return this.age; } }
Now we have defined our class Person. What if we wanted to define another class called Manager which inherits some properties from Person. There is no point redefining all this properties again when we define our Manager class, we can just set it to inherit from the class Person.
JavaScript doesn't have built in inheritance but we can use a technique to implement inheritance as follows:
Inheritance_Manager = {};//We create an inheritance manager class (the name is arbitrary)
Now let's give our inheritance class a method called extend which takes the baseClass
and subClassas arguments.
Within the extend method, we will create an inner class called inheritance function inheritance() { }
. The reason why we are using this inner
class is to avoid confusion between the baseClass
and subClass
prototypes.
Next we make the prototype of our inheritance class point to the baseClass
prototype as with the following code:
inheritance.prototype = baseClass. prototype;
Then we copy the inheritance prototype into the subClass
prototype as follows:
subClass.prototype = new inheritance();
The next thing is to specify the constructor for our subClass
as follows:
subClass.prototype.constructor = subClass;
Once finished with our subClass
prototyping, we can specify the next two lines of code to set some base class pointers.
subClass.baseConstructor = baseClass; subClass.superClass = baseClass.prototype;
Here is the full code for our extend function:
Inheritance_Manager.extend = function(subClass, baseClass) {
function inheritance() { }
inheritance.prototype = baseClass.prototype;
subClass.prototype = new inheritance();
subClass.prototype.constructor = subClass;
subClass.baseConstructor = baseClass;
subClass.superClass = baseClass.prototype;
}
Now that we have implemented our inheritance, we can start using it to extend our classes. In this case we are going to
extend our Person class into a Manager
class as follows:
We define the Manager
class
Manager = function(id, name, age, salary) { Person.baseConstructor.call(this, id, name, age); this.salary = salary; alert('A manager has been registered.'); }
we make it inherit form Person
Inheritance_Manager.extend(Manager, Person);
If you noticed, we have just called the extend method of our Inheritance_Manager
class and passed the subClass
Manager in our case and then the baseClass
Person. Note that the order is very important here. If you swap them, the inheritance
Will not work as you intended if at all.
Also note that you will need to specify this inheritance before you can actually define our subClass.
Now let us define our subClass:
We can add more methods as the one below. Our Manager class will always have the methods and properties defined in the Person
class because it inherits from it.
Manager.prototype.lead = function(){ alert('I am a good leader'); }
Now to test it let us create two objects, one from the class Person
and one from the inherited class Manager
:
var p = new Person(1, 'Joe Tester', 26); var pm = new Manager(1, 'Joe Tester', 26, '20.000');
Here is the full Javascript Code you can copy:
Person = function(id, name, age){
this.id = id;
this.name = name;
this.age = age;
alert('A new person has been accepted');
}
Person.prototype = {
/** wake person up */
wake_up: function() {
alert('I am awake');
},
/** retrieve person's age */
get_age: function() {
return this.age;
}
}
Inheritance_Manager = {};
Inheritance_Manager.extend = function(subClass, baseClass) {
function inheritance() { }
inheritance.prototype = baseClass.prototype;
subClass.prototype = new inheritance();
subClass.prototype.constructor = subClass;
subClass.baseConstructor = baseClass;
subClass.superClass = baseClass.prototype;
}
Manager = function(id, name, age, salary) {
Manager.baseConstructor.call(this, id, name, age);
this.salary = salary;
alert('A manager has been registered.');
}
Inheritance_Manager.extend(Manager, Person);
Manager.prototype.lead = function(){
alert('I am a good leader');
}
var p = new Person(1, 'Joe Tester', 26);
var pm = new Manager(1, 'Joe Tester', 26, '20.000');
You can manipulate this objects by accessing some of their properties as follows:
alert(p.name); alert(pm.salary);
Post Comment
ELNQOP There is definately a lot to learn about this subject. I love all the points you ave made.
nZmSoq Some truly prime articles on this site, saved to my bookmarks.
wpYhau This is a good tip especially to those new to the blogosphere. Simple but very precise info Many thanks for sharing this one. A must read article!
I definitely liked entire lot that was written.
I'd bent to regard reading more and more.
I liking be beneficial to learn as much as I can
I surely wonder you in behalf of the sequel so probably done recognition you unusually much for the time. Successfully!LW62vT Very nice article. I certainly appreciate this site. Stick with it!
online casino games
casinos online
online casino games
play casino games online
online casino gamesThanks very nice blog!
PSG fotballdrakter 2018casino online
slot online
casino slots
best online casino
online casinoslot online
kasino
slot game
casino online
online casino99 slot machines no deposit bonus
online casino philippines
slots hangout
online slots app
777 no deposit bonusslots
online casino
casino games
casino list
slotsonline casino
casino games
casino
casino online
casino onlinecasino online
casino online
casino online
casino
casino onlinecasino games
casino games slots
play casino games online
casino games real money
online casino real moneyonline casino
cassino
casino online
cassino
online casinoplay casino games online
casino games slots
online casino slots
casino games
online casinoloans in houston tx
short term loans
loan today
cash loans near me
loans for single motherscash casino kehl
free casino video slots cleopatra powerdaddy
gaming casino online
online roulette game india
casino 7777cassino download
online casino
slots zoo
no deposit bonus thunderbolt casino6S3a2A Your article is a refreshing change from the content I ave been reading on this topic. I agree with a lot of what you are saying here.
legit online loans
cash advance near me
instalment loansuw6W5n I really liked your article post.Really thank you! Much obliged.
doors.txt;2;4
It always seems impossible until it's done
MBH7lC Your style is unique in comparison to other people I ave read stuff from. Thanks for posting when you ave got the opportunity, Guess I will just bookmark this page.
501Plh I value the article post.Really looking forward to read more. Keep writing.
Nc54fB I think this is a real great post.Really looking forward to read more. Awesome.
KHyifW My brother rec?mmended I might like thаАабТТs websаАабТТte.
NYZgrV Thanks-a-mundo for the blog article.Really thank you!
VZXvS7 Really appreciate you sharing this article post.Thanks Again. Really Great.
n2c4zr
RRM4Pa I truly appreciate this post. I ave been looking all over for this! Thank goodness I found it on Bing. You have made my day! Thank you again.
5mJ1hX I truly appreciate this blog post.Really thank you! Cool.
KCrL62 It's really a cool and helpful piece of information. I am happy that you just shared this useful information with us. Please keep us informed like this. Thank you for sharing.
RiupFi Well I sincerely liked studying it. This subject offered by you is very helpful for proper planning.
xqF5Zw Hello, i think that i saw you visited my weblog so i came to ���return the favor���.I am trying to find things to improve my website!I suppose its ok to use a few of your ideas!!
yTwB2l Awesome blog.Really looking forward to read more. Awesome.
jQf9Ga Really enjoyed this post.Really looking forward to read more. Much obliged.
ikzUNT Hey, thanks for the blog.Thanks Again.
PIPUnh Very good post.Really thank you! Will read on...