-
Notifications
You must be signed in to change notification settings - Fork 0
/
Inheritance_relation_in_Functions_&_Constructor.js
229 lines (175 loc) · 5.45 KB
/
Inheritance_relation_in_Functions_&_Constructor.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Basically in this we are doing linkaging of the objects
/*
Ques.
Create a Person Function , Having Id, Name, Age, City
Create a Employee Function. Having Salary, Bonus, CompanyName
Make a RelationShip b/w Employee and Person
BY Using proto and prototype
Hint : Use Call
Think about how Chain RelationShip (Inheritance) and One Function Constructor call another Function Constructor like super constructor call.
*/
// Person function constrcutor
function Person(id, name, city){
this.id = id;
this.name = name;
this.city = city;
}
Person.prototype.printPersonDetails = function(){
console.log(`Id ${this.id} Name ${this.name} City ${this.city}`);
}
// Employee function constructor
function Employee(salary, bonus, dept){
this.salary = salary;
this.bonus = bonus;
this.dept = dept;
}
Employee.prototype.printEmployeeDetails = function(){
console.log(`Salary ${this.salary} Bonus ${this.bonus} Dept ${this.dept}`);
}
typeof Person;
'function'
typeof Person.prototype;
'object'
typeof Employee;
'function'
typeof Employee.prototype;
'object'
Employee.prototype.__proto__ == Object.prototype; // connected with GOD function
true
Employee.prototype.__proto__ == Person.prototype.__proto__; // both functions are connected with GOD function
true
Employee.prototype.__proto__ = Person.prototype // creating linkage.
{printPersonDetails: ƒ, constructor: ƒ}printPersonDetails: ƒ ()constructor: ƒ Person(id, name, city)[[Prototype]]: Object
Employee.prototype.__proto__ == Object.prototype; // now it's pointing to Person's Prototype instead of GOD function.
false
Employee.prototype.__proto__ == Person.prototype; // Linkage created...
true
// Updating employee function for creating linkage...
function Employee(id, name, city, salary, bonus, dept){ // id ,name, city (Give it to the Person)
console.log(' this is ', this);
//var p = new Person() // not using this, bcz we don't want to create object of person. We wan't to call person using employee Object.
Person.call(this, id, name, city); // bind this (Employee) with Person
this.salary = salary;
this.bonus = bonus;
this.dept = dept;
}
Employee.prototype.printEmployeeDetails = function(){
console.log(`Salary ${this.salary} Bonus ${this.bonus} Dept ${this.dept}`);
}
Employee.prototype.__proto__ == Person.prototype; // linkage broken..... bcz we have updated the function..
false
Employee.prototype.__proto__ = Person.prototype; // creating linkage again...
{printPersonDetails: ƒ, constructor: ƒ}
Employee.prototype.__proto__ == Person.prototype; // Linkage created...
true
var amit =new Employee(1001, 'amit','delhi',222222,2222,'IT');
this is Employee {}
amit; // now we are Able to access both the functions...
Employee {id: 1001, name: 'amit', city: 'delhi', salary: 222222, bonus: 2222, …}
amit.printEmployeeDetails(); // also able to access unique functions....
Salary 222222 Bonus 2222 Dept IT
amit.printPersonDetails(); // also able to access unique functions....
Id 1001 Name amit City delhi
amit;
Employee {id: 1001, name: 'amit', city: 'delhi', salary: 222222, bonus: 2222, …}
// chaining behind the scene..
amit.__proto__ == Employee.prototype;
true
amit.__proto__.__proto__ == Person.prototype;
true
amit.__proto__.__proto__.__proto__ == Object.prototype;
true
amit.__proto__.__proto__.__proto__.__proto__ == null;
true
// Problem is that we have to call both the functions...
amit.printEmployeeDetails();
Salary 222222 Bonus 2222 Dept IT
amit.printPersonDetails();
Id 1001 Name amit City delhi
// Resolving issue...
Employee.prototype.printEmployeeDetails = function(){
this.printPersonDetails();
console.log(`Salary ${this.salary} Bonus ${this.bonus} Dept ${this.dept}`);
}
amit.printEmployeeDetails();
Id 1001 Name amit City delhi
Salary 222222 Bonus 2222 Dept IT
/*
Ques.
Create a Person Function , Having Id, Name, Age, City
Create a Employee Function. Having Salary, Bonus, CompanyName
Make a RelationShip b/w Employee and Person
*/
// ES6 Class Style
class A{
}
typeof A;
'function'
class Person{
constructor(id, name, city){
this.id = id;
this.name = name;
this.city = city;
}
printPersonDetails(){
console.log(`Id ${this.id} Name ${this.name} City ${this.city}`);
}
}
class Employee extends Person{
constructor(id, name , city, salary, bonus, dept){
super(id, name, city);
this.salary = salary;
this.bonus = bonus;
this.dept = dept;
}
printEmployeeDetails(){
super.printPersonDetails();
console.log(`Salary ${this.salary} Bonus ${this.bonus} Dept ${this.dept}`);
}
}
typeof Person;
'function'
typeof Employee;
'function'
Person.prototype
{constructor: ƒ, printPersonDetails: ƒ}
Employee.prototype;
Person {constructor: ƒ, printEmployeeDetails: ƒ}
Employee.prototype.__proto__ == Person.prototype;
true
var amit =new Employee(1001, 'amit','delhi',222222,2222,'IT');
amit;
Employee {id: 1001, name: 'amit', city: 'delhi', salary: 222222, bonus: 2222, …}
amit.printEmployeeDetails();
Id 1001 Name amit City delhi
Salary 222222 Bonus 2222 Dept IT
// Extra concepts about ES6
class Q{
static show(){
console.log("Q Show ");
}
}
Q.show();
Q Show
typeof Q;
'function'
function T(){
console.log("T ");
}
T.show= function(){
console.log("SHow ");
}
T.show();
SHow
var w = new Object();
w;
{}
var f = new Function();
f;
ƒ anonymous(
) {
}
typeof f;
'function'
typeof w;
'object'