Javascript practice Day 3 : inheritance
Inheritance in javascript is achieved by prototyping the object of base class into the child class . Chile class object will have access to all the methods of the base class , If child class object refers a method , it is first checked into child class and then into base class.
function Employee(name)
{
this.name= name;
}
Employee.prototype.getName = function(){
return this.name;
}
Employee.prototype.getNameLength = function(){
return this.getName().length;
}
var emp1= new Employee("John");
function Manager(salary)
{
this.salary= salary;
}
//Inheritence is happening here Employee object is assigned to Manager
Manager.prototype=emp1;
//Manager obect is created and it can access the properties of the Employee class
var memp=new Manager(2000);
document.write("Name : "+memp.getName()+"<br />");
document.write("Length Name : "+memp.getNameLength()+"<br />");
document.write("Salary : "+memp.salary +"<br />");
function Employee(name)
{
this.name= name;
}
Employee.prototype.getName = function(){
return this.name;
}
Employee.prototype.getNameLength = function(){
return this.getName().length;
}
var emp1= new Employee("John");
function Manager(salary)
{
this.salary= salary;
}
//Inheritence is happening here Employee object is assigned to Manager
Manager.prototype=emp1;
//Manager obect is created and it can access the properties of the Employee class
var memp=new Manager(2000);
document.write("Name : "+memp.getName()+"<br />");
document.write("Length Name : "+memp.getNameLength()+"<br />");
document.write("Salary : "+memp.salary +"<br />");
Comments
Post a Comment