Link for all dot net and sql server video tutorial playlists
http://www.youtube.com/user/kudvenkat/playlists
Link for slides, code samples and text version of the video
http://csharp-video-tutorials.blogspot.com/2015/02/inheritance-in-javascript.html
In this video we will discuss Inheritance in JavaScript with an example.
Object oriented programming languages support inheritance. Since JavaScript is also an object oriented programming language, it supports inheritance.
In Object oriented programming languages like C# and Java to implement inheritance, a class inherits from another class. In JavaScript, we don’t have the concept of classes, so inheritance in JavaScript is prototype-based. This means to implement inheritance in JavaScript, an object inherits from another object. Let us understand this with an example.
// Employee will be the base object (Similar to base class in c#)
var Employee = function (name)
{
this.name = name;
}
// getName() function is added to the base object (Employee)
Employee.prototype.getName = function ()
{
return this.name;
}
// PermanentEmployee will be the derived object
var PermanentEmployee = function (annualSalary)
{
this.annualSalary = annualSalary;
}
// Use prototype to associate Employee as the base object for PermanentEmployee
PermanentEmployee.prototype = new Employee(“Mark”);
var pe = new PermanentEmployee(50000);
// Derived object (permanentEmployee) can see the base object (Employee) getName() method
document.write(pe.getName());
alert(pe instanceof Employee); // Returns true
alert(pe instanceof PermanentEmployee); // Returns true
Notice that the derived object (PermanentEmployee) can see the base object (Employee) getName() method. When getName() method is called, JavaScript first tries to find this method in the derived object (). It can’t find the method there so it goes to the parent object and finds it there.
If you add a new method to the parent object, it becomes available in the derived object.
var Employee = function (name)
{
this.name = name;
}
Employee.prototype.getName = function ()
{
return this.name;
}
// Adding getNameLength() method to the parent object
// which becomes available in the derived object
Employee.prototype.getNameLength = function ()
{
return this.name.length + ” characters”;
}
// PermanentEmployee will be the derived object
var PermanentEmployee = function (annualSalary)
{
this.annualSalary = annualSalary;
}
PermanentEmployee.prototype = new Employee(“Mark”);
var pe = new PermanentEmployee(50000);
// Call getNameLength() method added to the parent object
document.write(pe.getNameLength()); // Output : 4 characters
Use hasOwnProperty() method to determine if a property is defined on the actual object or it’s prototype. Here is an example.
var Employee = function (name)
{
this.name = name;
}
var PermanentEmployee = function (annualSalary)
{
this.annualSalary = annualSalary;
}
var employee = new Employee(“Mark”);
PermanentEmployee.prototype = employee;
var pe = new PermanentEmployee(50000);
document.write(“Employee.name: ” + employee.hasOwnProperty(‘name’));
document.write(“Employee.annualSalary: ” + employee.hasOwnProperty(‘annualSalary’));
document.write(“PermanentEmployee.name: ” + pe.hasOwnProperty(‘name’));
document.write(“PermanentEmployee.annualSalary: ” + pe.hasOwnProperty(‘annualSalary’));
Output :
Employee.name: true
Employee.annualSalary: false
PermanentEmployee.name: false
PermanentEmployee.annualSalary: true
Original source
18 responses to “Inheritance in JavaScript”
How to Use Inheritance in Dom Thanks
Is there any other way to do Inheritance without using prototype in JavaScript?
Thank you so much!
Thank you! It's a very good tutorial.
Hi Kudvenkat, this is one of the best tutorial on JavaScript tutorial I have gone through, nice work!!
One question, which one is the best technique? Is there any fundamental difference between these two?
var employee = new Employee("Mark");
PermanentEmployee.prototype = employee;
VS
PermanentEmployee.prototype = Object.create(Employee.prototype);
@3:42 5,000 = 50,000… I'm glad you don't write my paycheck :o)
Great Work! Really appreciate the effort as I came to know abt lots of new things.
In case of inheritance, you may have missed out "BaseClass.call()" part which is quite important.
For instance:
var Employee = function(name) {
this.name = name;
}
Employee.prototype.doStuff = function() {
//do something
}
var PermanentEmployee= function(name) {
Employee.call(name);
Employee.prototype.doOtherStuff = function() {
//do something
}
Very nice. Easily get understand.
If somebody will remove your videos from youtube i think i would diee mate 🙂
hi venkat sir,
im new in UI field tell me sir this much js is enough or need to learn oojs also
giude me friends
thanks mate
Thanks Venkat…You are a great Mentor
Hey, i think you should look this documentation, i´m a little lost about the inheritance but I understand if you set the prototype in this way you´ll overwrite all methods of the son.
HI VENKAT ,
How are you?
You are doing great job ,nice vedios, All the best.
Will you please upload administrator part of sql…
Thank You in advance….
You are great!! greetings from Costa Rica! Pura vida!!!
thank u sir..!
Great! I appreciate a lot what you share… greetings from Mexico!
Good work… congratulations from spain