-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathPrototype-and-Prototype-inheritance.js
More file actions
130 lines (112 loc) · 4.29 KB
/
Copy pathPrototype-and-Prototype-inheritance.js
File metadata and controls
130 lines (112 loc) · 4.29 KB
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
/* 💡"JavaScript-with-JC"
👉 Prototype and Prototype Inheritance
The prototype is an object that is associated with every functions and objects by default in JavaScript,
Every object includes __proto__ property that points to prototype object of a function that created the object.
💡Difference between prototype and __proto__
prototype is a property of a Function object. It is the prototype of objects constructed by that function.
__proto__ is an internal property of an object, pointing to its prototype.
*/
// 👉 Prototype
// function constructor, Person.prototype => Object.prototype => null
const Person = function (name, age) {
// instance members created for each object separately consume extra memory
this.name = name;
this.age = age;
this.getName = function () {
console.log("name is", this.name);
};
};
const jayesh = new Person("jayesh", 24);
// jayesh.__proto__ => Person.prototype => Object.prototype => null
console.log("jayesh", jayesh);
/* output
age: 24;
getName: ƒ(); => method created separately for jayesh object consume extra memory
name: "jayesh";
*/
jayesh.getName(); // name is jayesh
console.log(jayesh.__proto__); // Person.prototype
console.log(jayesh.__proto__ === Person.prototype); // true
console.log(Object.getPrototypeOf(jayesh) === Person.prototype); // true
console.log(jayesh.__proto__.__proto__); // Object.prototype
console.log(jayesh.__proto__.__proto__.__proto__); // null
const sam = new Person("sam", 25);
// sam.__proto__ => Person.prototype => Object.prototype => null
console.log("sam", sam);
/* output
age: 25;
getName: ƒ(); => method created separately for sam object consume extra memory
name: "sam";
*/
sam.getName(); // name is sam
// Now, Let's create prototype member ( common parent inherit member for all objects ) saves memory.
Person.prototype.getAge = function () {
console.log("age is", this.age);
};
console.log("After adding getAge fn as Prototype member");
console.log("jayesh", jayesh);
/* output
age: 24;
getName: ƒ(); => method created separately for jayesh object consume extra memory
name: "jayesh";
*/
console.log("sam", sam);
/* output
age: 25;
getName: ƒ(); => method created separately for sam object consume extra memory
name: "sam";
*/
console.log("Person.prototype", Person.prototype);
/*output
getAge: ƒ () => common method sharable with jayesh and sam object saves memory
constructor: ƒ (name, age) => function constructor
[[Prototype]]: Object => Object.Prototype => null
*/
// only one copy of getAge() will be created inside Person.prototype
jayesh.getAge(); // age is 24
sam.getAge(); // age is 24
// now let's add one new property to jayesh
jayesh.lastName = "Choudhary";
console.log(jayesh);
/* output
age: 24
getName: ƒ ()
lastName: "Choudhary" // new property added only in jayesh obj not in Person.prototype
name: "jayesh"
*/
// now let's check how to get all keys of object
// 1) get instance members key
console.log(Object.keys(jayesh));
// ['name', 'age', 'getName', 'lastName']
// 2) get Instance + prototype members key (Recommended)
for (let key in jayesh) {
console.log(key);
}
// name, age, getName, lastName, getAge(Prototype member)
// Now, Let's see Prototype inheritance
// Parent function contructor Parent.prototype => Object.prototype => null
const Parent = function (name, age) {
//instance member
this.name = name;
this.age = age;
};
// adding prototype member to Parent
Parent.prototype.getNameAge = function () {
console.log("name", this.name, "age", this.age);
};
// creating object of Parent function constructor
const joseph = new Parent("joseph", 35);
joseph.getNameAge(); // name joseph age 35
// Child function contructor Child.prototype => Object.prototype => null
const Child = function (name, age, isCrying) {
// function constructor borrowing using call method (inheriting instance members)
Parent.call(this, name, age);
this.isCrying = isCrying;
};
// Now we have to make Child.prototype points to Parent.Prototype (inheriting prototype members)
Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;
// After above 2 steps, Child.prototype => Parent.prototype => Object.prototype => null
const josephBaby = new Child("josephBaby", 3, true);
console.log(josephBaby);
josephBaby.getNameAge(); // name josephBaby age 3, child is inheriting the property getNameAge of parent.