post-ad

JavaScript Supports Function Override and Inheritance

JavaScript Function Override

Overriding is supported by Javascript, although overloading is not. The last function is referred to by the Javascript engine when numerous functions are defined in Javascript and included in a file.

the primary Javascript file to which we refer

function Employee(name)  
{  
    this.Name=name;     
}  
  
Employee.prototype.getgreeting=function(){  
    return "Hello, "+this.Name;  
}

We now wish to override the getgreeting method in accordance with our requirements. Therefore, we create a second file, override this method using the code below, 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())
JavaScript’s use of inheritance

We now wish to override the getgreeting method in accordance with our requirements. Therefore, we create a second file, override this method using the code below, and include both files in our index.html file.

Code Reuse is the basic goal of inheritance. We construct a parent class in Java or C#, and the child class inherits from it. However, creating a prototype in Javascript allows you to accomplish this kind of functionality. As a result, Javascript inheritance is prototype-based. This object inheritance may be put into practise 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());

In the code above, we build two function Object() { [native code] } functions: one for the temporary employee, and another for the permanent employee.

So that attributes in the employee function Object() { [native code] } method may access a permanent employee object, we next build an object of type Employee and assign it to the PermanentEmployee prototype object.

per instanceof Employee

You can determine whether anything is an instance of Employee or not by using this code, and the outcome is true.

A permanent employee object can access any method you introduced to the parent function Object() { [native code] } function.

If a property belongs to a parent or child, it may be determined using the function hasOwnProperty() { [native code] } method.

// 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());  
document.writeln(per instanceof Employee)  
  
document.writeln("<br/>")  
document.writeln("annualSalary property : "+per.hasOwnProperty("annualSalary"))  
  
document.writeln("<br/>")  
document.writeln("Name property : "+emp.hasOwnProperty("Name"))

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories

page-ad