-
Notifications
You must be signed in to change notification settings - Fork 0
/
inheritance.js
102 lines (78 loc) · 2.89 KB
/
inheritance.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*-------------------------------------------- a class is blueprint of object --------------------------------------------*/
class Employee {
constructor(name, doj) {
this.name = name;
this.doj = doj;
this.__proto__.biro = "kauaa"; // will be added to prototype object of this class
}
getName() {
console.log("name is " + this.name);
}
getName2 = () => {
console.log("name is " + this.name);
}
getName3 = function () {
console.log("name is " + this.name)
}
getDateOfJoining() {
console.log("date of joining is " + this.doj);
}
static add(a, b) {
return a + b;
}
}
Employee.prototype.biro2 = "bauaa";
// const emp = new Employee("aditya", "03 Aug 2020"); //emp is object of Employee class i.e emp is created using blueprint of Employee
// emp.getName();
// emp.getDateOfJoining();
// console.log("biro", emp.__proto__.biro2)
class Programmer extends Employee {
constructor(name, doj, team, language) {
super(name, doj); // to inherit properties from Employee class
this.team = team;
this.language = language;
}
getLanguage() {
console.log("langauge is: " + this.language)
}
}
// const sde1 = new Programmer("adarsh", "03 March 2020", "assessment", "javaScript");
// sde1.getLanguage()
// console.log("sde1", sde1);
/*-------------------------------------------- function can also be used as blueprint of object --------------------------------------------*/
function EmployeeFunc(name, doj) {
this.name = name;
this.doj = doj;
this.__proto__.kauaa = "kauaa is a bird";
this.getName = function () {
console.log(this.name);
}
this.getDateOfJoining = function () {
console.log(this.doj);
}
}
EmployeeFunc.prototype.biro = "kauaa"; // will be added to prototype object of this object
EmployeeFunc.prototype.setName = function (name) {
this.name = name;
}
// const emp1 = new EmployeeFunc("aditya", 2123);
// emp1.getName();
// console.log("biro", emp1.__proto__.biro)
// console.log("kauaa", Object.getPrototypeOf(emp1))
function ProgrammerFunc(name, doj, team, language) {
EmployeeFunc.call(this, name, doj); //parent function is called in child function to inherit
this.team = team;
this.language = language;
this.getLang = function () {
console.log(this.language);
}
}
ProgrammerFunc.prototype = Object.create(EmployeeFunc.prototype); // to inherit parent's prototype in children
ProgrammerFunc.prototype.hiFrnd = "nice"; // adding hiFrnd key in prototype object of ProgrammerFunc
const programmerFunc = new ProgrammerFunc("iphone", "15 Oct 2020", "ios 16", "swift");
console.log("programmerFunc", programmerFunc);
console.log("programmerFunc", programmerFunc.doj);
programmerFunc.getName()
programmerFunc.getLang()
console.log(Object.getPrototypeOf(programmerFunc))
console.log(programmerFunc.__proto__)