Skip to content

Commit 26d88d3

Browse files
committed
add proto chaining
1 parent 68c4d14 commit 26d88d3

File tree

5 files changed

+104
-12
lines changed

5 files changed

+104
-12
lines changed

session 1/chapters/3. Prototypes.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ function Human(firstName, lastName) {
1313

1414
var person1 = new Human('Virat', 'Kohli');
1515
var person2 = new Human('Sachin', 'Tendulkar');
16+
17+
person1.__proto__.constructor === Human.prototype.constructor; //true
1618
```
1719

1820
![problemWithObjectConstructor](../images/ProblemWithConstructorrObjectCreation.jpg)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Prototype Chaining, the way to achieve Inheritance in Javascript
2+
3+
JavaScript does not have classes unlinke other languages. It uses the concept of prototypes for inheritance
4+
5+
Prototype chaining means an objects dunder proto or proto will point to another object instead of pointing to the constructor function prototype.
6+
7+
If the other object's dunder proto or proto property points to another object it will results into chain. This is prototype chaining.
8+
9+
![protoChain](../images/protoChain.jpg)
10+
11+
```javascript
12+
//SuperType constructor function
13+
function SuperType() {
14+
this.name = 'Virat';
15+
}
16+
17+
//SuperType prototype
18+
SuperType.prototype.getSuperName = function () {
19+
return this.name;
20+
};
21+
22+
//SubType prototype function
23+
function SubType() {
24+
this.age = 26;
25+
}
26+
27+
//Inherit the properties from SuperType
28+
SubType.prototype = new SuperType();
29+
30+
//Add new property to SubType prototype
31+
SubType.prototype.getSubAge = function () {
32+
return this.age;
33+
};
34+
35+
//Create a SubType object
36+
var subTypeObj = new SubType();
37+
console.log('subTypeObj.name', subTypeObj.name); //Output: Virat
38+
console.log('subTypeObj.age', subTypeObj.age); //Output: 26
39+
console.log('subTypeObj.getSuperName()', subTypeObj.getSuperName()); //Output: Virat
40+
console.log('subTypeObj.getSubAge()', subTypeObj.getSubAge()); //Output: 26
41+
```
42+
43+
**Note: New methods must be added to the SubType after the inheritance because inheritance overwrites the existing prototype of SubType**
44+
45+
![inheritance](../images/inheritance.jpg)
46+
47+
# Problems with prototype chaining
48+
49+
As all the properties of the super type prototype are shared among the child objects, if one child modifies the property of the Super type prototype, other childs also gets affected. This issue has been explined in greate details here
50+
51+
To fix this issue, we use constructor to inherit the instance properties and prototype chaining to to inherit methods and share properties

session 1/images/inheritance.jpg

59.6 KB
Loading

session 1/images/protoChain.jpg

39.3 KB
Loading

session 1/server/index.js

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,56 @@
1-
function Foo() {}
1+
//SuperType constructor function
2+
function SuperType() {
3+
this.name = 'Virat';
4+
}
25

3-
const dunderProtoEqualsPrototypeOfFunction =
4-
new Foo().__proto__ === Foo.prototype;
6+
//SuperType prototype
7+
SuperType.prototype.getSuperName = function () {
8+
return this.name;
9+
};
510

6-
const actualObjectDontHavePrototype = new Foo().prototype === undefined;
11+
//SubType prototype function
12+
function SubType() {
13+
this.age = 26;
14+
}
715

8-
console.log(
9-
'dunder Proto Equals Prototype Of Function',
10-
dunderProtoEqualsPrototypeOfFunction
11-
);
16+
//Inherit the properties from SuperType
17+
SubType.prototype = new SuperType();
1218

13-
console.log('actual Object Dont Have Prototype', actualObjectDontHavePrototype);
19+
//Add new property to SubType prototype
20+
SubType.prototype.getSubAge = function () {
21+
return this.age;
22+
};
1423

15-
const fooObject = new Foo();
16-
console.log('fooObject is instance of Foo', fooObject instanceof Foo);
17-
console.log('fooObject is instance of Object', fooObject instanceof Object);
24+
//Create a SubType object
25+
var subTypeObj = new SubType();
26+
console.log('subTypeObj.name', subTypeObj.name); //Output: Virat
27+
console.log('subTypeObj.age', subTypeObj.age); //Output: 26
28+
console.log('subTypeObj.getSuperName()', subTypeObj.getSuperName()); //Output: Virat
29+
console.log('subTypeObj.getSubAge()', subTypeObj.getSubAge()); //Output: 26
30+
31+
console.log('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
32+
33+
var subTypeObj2 = new SubType();
34+
console.log('2-subTypeObj.name', subTypeObj2.name); //Output: Virat
35+
console.log('2-subTypeObj.age', subTypeObj2.age); //Output: 26
36+
console.log('2-subTypeObj.getSuperName()', subTypeObj2.getSuperName()); //Output: Virat
37+
console.log('2-subTypeObj.getSubAge()', subTypeObj2.getSubAge()); //Output: 26
38+
39+
console.log('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
40+
41+
subTypeObj2.name = 'akash';
42+
console.log('=======after changing the name property==========');
43+
console.log('2-subTypeObj.name', subTypeObj2.name); //Output: Virat
44+
console.log('subTypeObj.name', subTypeObj.name); //Output: Virat
45+
console.log('2-subTypeObj.getSuperName()', subTypeObj2.getSuperName()); //Output: Virat
46+
console.log('subTypeObj.getSuperName()', subTypeObj.getSuperName()); //Output: Virat
47+
48+
console.log('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
49+
50+
var newObj = new SuperType();
51+
newObj.__proto__.getSuperName = () => {
52+
return 'changed function definition';
53+
};
54+
55+
console.log('2-subTypeObj.getSuperName()', subTypeObj2.getSuperName()); //Output: Virat
56+
console.log('subTypeObj.getSuperName()', subTypeObj.getSuperName()); //Output: Virat

0 commit comments

Comments
 (0)