Override Function And Inheritance In JavaScript

Overriding JavaScript Function

 
Javascript supports overriding but not overloading. When you define multiple functions in Javascript and include them in the file then the last one is referred by the Javascript engine.
 Override Function And Inheritance In JavaScript
 
Original Javascript file which we refer:
  1. function Employee(name)  
  2. {  
  3.     this.Name=name;     
  4. }  
  5.   
  6. Employee.prototype.getgreeting=function(){  
  7.     return "Hello, "+this.Name;  
  8. }  
Now as per our requirement we want to override the getgreeting function.  So  we create a new file and use the below code to override this function and include both files in our index.html file. 
  1. Employee.prototype.getgreeting=function(){  
  2.     return this.Name.toUpperCase();  
  3. }  
  4.   
  5. var emp=new Employee("xyz");  
  6. alert(emp.getgreeting())  

Inheritance In JavaScript

 
Object-oriented programming languages like C# and Java support inheritance, and Javascript is also an object-oriented programming language so it supports inheritance.
 
The main purpose of inheritance is Code Reuse. In Java or C# we create a parent class and that inherits in the child class. But in Javascript, you can achieve this kind of functionality by using a prototype. So inheritance in Javascript is prototype-based. We can implement this object inheritance from another object. 
  1. // this is constructor function  
  2. var Employee=function(name)  
  3. {  
  4.     this.Name=name;  
  5. }  
  6.   
  7. Employee.prototype.getname=function(){  
  8.     return this.Name;  
  9. }  
  10.   
  11. var PermanantEmployee=function(salary){  
  12.     this.annualSalary=salary;  
  13. }  
  14.   
  15. var emp=new Employee("Sagar Jaybhay");  
  16. PermanantEmployee.prototype =emp; // in this case employee object is parent of permanant employee  
  17.   
  18. var per=new PermanantEmployee(3000);  
  19. console.log(per.getname());  
  20.   
  21. document.writeln(per.getname());  
In the above code, we create Employee as a constructor function and another constructor function which is a permanent employee.
 
Then we create an object of Employee and assign this object to the PermanentEmployee prototype object so properties in the employee constructor function can access a permanent employee object.  
  1. per instanceof Employee  
By using this code you can check whether it is an instance of Employee or not and the result is true.
 
Whatever method you added to parent constructor function is accessible to a permanent employee object.
 
hasOwnProperty method is used to determine if the property belongs to parent or child.  
  1. // this is constructor function  
  2. var Employee=function(name)  
  3. {  
  4.     this.Name=name;  
  5. }  
  6.   
  7. Employee.prototype.getname=function(){  
  8.     return this.Name;  
  9. }  
  10.   
  11. var PermanantEmployee=function(salary){  
  12.     this.annualSalary=salary;  
  13. }  
  14.   
  15. var emp=new Employee("Sagar Jaybhay");  
  16. PermanantEmployee.prototype =emp; // in this case employee object is parent of permanant employee  
  17.   
  18. var per=new PermanantEmployee(3000);  
  19. console.log(per.getname());  
  20.   
  21. document.writeln(per.getname());  
  22. document.writeln(per instanceof Employee)  
  23.   
  24. document.writeln("<br/>")  
  25. document.writeln("annualSalary property : "+per.hasOwnProperty("annualSalary"))  
  26.   
  27. document.writeln("<br/>")  
  28. document.writeln("Name property : "+emp.hasOwnProperty("Name"))