// This is a prototype , ye to become object
const proto = {
// slogan : function(){
// return "this compnay is worst"
// },
changeName : function(newName){
this.name = newName
}
}
// creating harry object with the help of above prototype
// 1st way
const harry = Object.create(proto)
harry.name = "harry";
harry.role ="engineer";
harry.age = 24
harry.changeName("harry2");
// creating harry object with the help of above prototype
// 2nd way
const himanshu = Object.create(proto,{
name : {value : "harry2ndway",writable: true},
role : {value : "programmer2ndway"}
})
himanshu.changeName("harry.....");
/* -------------prototype inheritance------------------------ */
// #employee constrctor
function Employee(name,sal,exp){
this.name = name;
this.salary = sal;
this.experience = exp;
}
// creating slogan function and adding in employee prototype
Employee.prototype.slogan = function(){
return `this company is of ${this.name}`
}
// creating object of employee with the help of above constructor
let employeeObj = new Employee('harry',2345456,34);
console.log(employeeObj);
console.log(employeeObj.slogan());
// #Programmer constrctor (inherit from employee constructor)
function Programmer(name,sal,exp,lang){
Employee.call(this,name,sal,exp);
this.language = lang;
}
// #inheriting employee prototype(slogan function) to programmer object
Programmer.prototype = Object.create(Employee.prototype)
// manually set constructor
Programmer.prototype.constructor = Programmer;
let programmerObj = new Programmer("rohan",2, 0 ,"java script")
console.log(programmerObj);
0 Comments