-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprototype-chaining.js
73 lines (62 loc) · 2.55 KB
/
prototype-chaining.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
/**
* Things to remember:
* - agument: The process of creating property or methods or assigning object to prototype.
* - side effect: While agumentating with another object the source object's construcutor will be overrided with agumented object.
* So the constructor need to be reset with its own object.
* - lookup : The process of searching the property in each objects
* - prototype chain: Look up process takes place in its parents
*/
function Shape() {
this.name = "Shape";
this.toString = function() {
return this.name;
};
}
function TwoDShape() {
this.name = "2D Shape";
}
function Triangle(side, height) {
this.name = "Triangle";
this.side = side;
this.height = height;
this.getArea = function() {
return this.side * this.height / 2;
};
}
console.log("Before Inheritance :-");
console.log("Shape.prototype",Shape.prototype);
console.log("TwoDShape.prototype ",TwoDShape.prototype);
console.log("Triangle.Prototype",Triangle.prototype);
//Augment
//We can either Augment to the empty prototype object or completely
//replace with another object
/* Shape.prototype.profession = "javascript developer";
Shape.prototype.writeCode = function() {
return "Writes Javascript Code";
}
console.log("Shape prototype",Shape.prototype);*/
console.log("Inheritance Begins here:-");
//Inheritance starts here
TwoDShape.prototype = new Shape();
Triangle.prototype = new TwoDShape();
//Before Resetting the constructor Property (Sideeffect demonstrated here)
console.log("\nBefore Resetting the constructor Property:-\n");
var twod = new TwoDShape();
var ta = new Triangle(5,10);
// Side Effect of replacing an object with prototype
// Example here twod object constructor points to Shape Constructor (this is the sideeffect)
console.log("Constructor of twod object" ,twod.constructor);
console.log("Constructor of ta object" ,ta.constructor);
//After Resetting the consructor Property
// Now the constructor property of twod and ta objects points to their
// respective TwoDShape and Triangle Constructors
TwoDShape.prototype.constructor = TwoDShape;
Triangle.prototype.constructor = Triangle;
console.log("\nAfter Resetting the consructor Property \n");
console.log("Constructor of twod object" ,twod.constructor);
console.log("Constructor of ta object" ,ta.constructor);
//Lets see the inheritance magic here
console.log("\nLets see how inheritance works now\n");
console.log("twod.toString() takes its own name ", twod.toString()); //Takes its own property
console.log("ta.toString takes its own name ", ta.toString());
console.log("ta.getArea ", ta.getArea());