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.

Original Javascript file which we refer:
- function Employee(name)
- {
- this.Name=name;
- }
- Employee.prototype.getgreeting=function(){
- return "Hello, "+this.Name;
- }
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.
- Employee.prototype.getgreeting=function(){
- return this.Name.toUpperCase();
- }
- var emp=new Employee("xyz");
- 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.
- // this is constructor function
- var Employee=function(name)
- {
- this.Name=name;
- }
- Employee.prototype.getname=function(){
- return this.Name;
- }
- var PermanantEmployee=function(salary){
- this.annualSalary=salary;
- }
- var emp=new Employee("Sagar Jaybhay");
- PermanantEmployee.prototype =emp; // in this case employee object is parent of permanant employee
- var per=new PermanantEmployee(3000);
- console.log(per.getname());
- document.writeln(per.getname());

Join the conversation! Your thoughts help the community grow.