Friday, September 21, 2018

OOPS in Javascript

//prototype function
//1st approach
function Employee(name){
this.name=name;
}
Employee.prototype.getName = function(){
return this.name;
}
var e1 = new Employee("jitendra");
var e2 = new Employee("Raju");
console.log(e1.getName()); //Jitendra
console.log(e2.getName()); //Raju

//2nd approach
function Employee(name){
this.name=name;
}
Employee.prototype.getName = function(){
return this.name;
}

function getEmployeeDetails(){
//Overriding getName
Employee.prototype.getName = function(){
return this.name.toUpperCase();
}
var e1 = new Employee("jitendra");
var e2 = new Employee("Raju");
console.log(e1.getName()); //JITENDRA
console.log(e2.getName()); //RAJU
}
getEmployeeDetails();

//Overriding alert
var alert = function(msg){
console.log(msg);
}
alert("hello jit");

//Inheritance in javascript
var Employee = function(name){
this.name = name;
}
Employee.prototype.getName = function() {
return this.name;
}
Employee.prototype.getNameLength = function() {
return this.name.length;
}

var PermanentEmployee = function(annualSalary){
this.annualSalary = annualSalary;
}
var employee = new Employee("Jitendra");
PermanentEmployee.prototype = employee;

var pe = new PermanentEmployee(5000);
console.log(pe.getName()) //Jitendra
console.log(pe.annualSalary); //5000
console.log(pe.getNameLength()); //8

console.log(pe instanceof Employee); //true
console.log(pe instanceof PermanentEmployee); //true

console.log("employee.name" + employee.hasOwnProperty('name')); //true
console.log(employee.hasOwnProperty('annualSalary')); //false


//Abstract Class in Javascript
var Shape = function(){
this.ShapeName="none";
throw new Error("Can not create a instance of an abstarct class !!");
}
Shape.prototype.draw = function() {
return "Drawing " + this.ShapeName;
}
var Circle = function(shapeName){
this.ShapeName = shapeName;
}

Circle.prototype= Object.create(Shape.prototype);
var circle = new Circle("Shape1");
console.log(circle.draw()); //Drawing Shape1



//Plymorphism in javscript
var Shape = function (){}
Shape.prototype.draw = function(){
return "I am a generic shape";
}

var Circle = function(){}
Circle.prototype = Object.create(Shape.prototype);
Circle.prototype.draw = function(){
return "I am a circle";
}

var Square = function(){}
Square.prototype = Object.create(Shape.prototype);
Square.prototype.draw = function(){
return "I am a Square";
}

var Traingle = function(){}
Traingle.prototype = Object.create(Shape.prototype);
Traingle.prototype.TraingleName = function (name){
return name;
}

var shapes = [new Shape(), new Circle() , new Square(), new Traingle()];
shapes.forEach(function (shape){
console.log(shape.draw());
});

//Output
//I am a generic shape
//I am a circle
//I am a Square
//I am a generic shape














No comments:

Post a Comment

Find the value from array when age is more than 30

 const data = [   { id: 1, name: 'Alice', age: 25 },   { id: 2, name: 'Bob', age: 30 },   { id: 3, name: 'Charlie', ...