Skip to content

Commit 18387f3

Browse files
committed
add problems with proto
1 parent 3af9173 commit 18387f3

File tree

6 files changed

+136
-33
lines changed

6 files changed

+136
-33
lines changed

session 1/chapters/3. Prototypes.md

Lines changed: 112 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
# What’s Prototype?
2-
3-
Almost all objects in JavaScript have the prototype property. By using it and more specifically the _prototype chain_ we can mimic inheritance.
4-
5-
The prototype is a reference to another object and it is used whenever JS can’t find the property you’re looking for on the current object.
6-
7-
Simply put, whenever you call a property on an object and it doesn’t exist, JavaScript will go to the prototype object and look for it there. This can bubble up all the way to Object.prototype before returning undefined. This is the essence of the prototype chain and the behavior that sits behind JavaScript’s inheritance.
8-
91
# Problem with creating objects with the constructor function:
102

113
Consider the constructor function below:
@@ -25,4 +17,116 @@ var person2 = new Human('Sachin', 'Tendulkar');
2517

2618
![problemWithObjectConstructor](../images/ProblemWithConstructorrObjectCreation.jpg)
2719

20+
When a function is created in JavaScript, the JavaScript engine adds a `prototype` property to the function. This prototype property is an object (called a prototype object) that has a constructor property by default.
21+
22+
The constructor property points back to the function on which `prototype` object is a property. We can access the function’s prototype property using `functionName.prototype`.
23+
24+
![functionObjPrototype](../images/functionObjPrototype.jpg)
25+
26+
![sharedProto](../images/sharedProto.jpg)
27+
28+
## Prototype Object or Prototype
29+
30+
Almost all objects in JavaScript have the prototype property. By using it and more specifically the _prototype chain_ we can mimic inheritance.
31+
32+
The prototype is a reference to another object and it is used whenever JS can’t find the property you’re looking for on the current object.
33+
34+
Simply put, whenever you call a property on an object and it doesn’t exist, JavaScript will go to the prototype object and look for it there. This can bubble up all the way to Object.prototype before returning undefined. This is the essence of the prototype chain and the behavior that sits behind JavaScript’s inheritance.
35+
36+
As a prototype object is an object, we can attach properties and methods to the prototype object. Thus, enabling all the objects created using the constructor function to share those properties and methods.
37+
38+
```javascript
39+
//Dot notation
40+
Human.prototype.name = 'Ashwin';
41+
console.log(Human.prototype.name); //Output: Ashwin
42+
43+
//Square bracket notation
44+
Human.prototype['age'] = 26;
45+
console.log(Human.prototype['age']); //Output: 26
46+
47+
console.log(Human.prototype);
48+
```
49+
50+
![humanProtoAdditionProps](../images/humanProtoAdditionProps.jpg)
51+
52+
# Problem with creating objects with the Prototype:
53+
54+
```javascript
55+
//Create an empty constructor function
56+
function Person() {}
57+
//Add property name, age to the prototype property of the Person constructor function
58+
Person.prototype.name = 'Ashwin';
59+
Person.prototype.age = 26;
60+
Person.prototype.sayName = function () {
61+
console.log(this.name);
62+
};
63+
64+
//Create an object using the Person constructor function
65+
var person1 = new Person();
66+
var person2 = new Person();
67+
68+
//Access the name property using the person object
69+
console.log('person1.name ', person1.name); // Output" Ashwin
70+
console.log(`person2.name `, person2.name); // Output" Ashwin
71+
72+
person1.name = 'john';
73+
74+
console.log('person1.name ', person1.name); // Output" Ashwin
75+
console.log(`person2.name `, person2.name); // Output" Ashwin
76+
```
77+
78+
In the following example, **if the intention is to have an array shared by all instances**, then this outcome is okay. But here this was not the case.
79+
80+
```javascript
81+
//Create an empty constructor function
82+
function Person() {}
83+
//Add property name, age to the prototype property of the Person constructor function
84+
Person.prototype.name = 'Ashwin';
85+
Person.prototype.age = 26;
86+
(Person.prototype.friends = ['Jadeja', 'Vijay']), //Arrays are of reference type in JavaScript
87+
(Person.prototype.sayName = function () {
88+
console.log(this.name);
89+
});
90+
91+
//Create objects using the Person constructor function
92+
var person1 = new Person();
93+
var person2 = new Person();
94+
95+
//Add a new element to the friends array
96+
person1.friends.push('Amit');
97+
98+
console.log(person1.friends); // Output: "Jadeja, Vijay, Amit"
99+
console.log(person2.friends); // Output: "Jadeja, Vijay, Amit"
100+
```
101+
102+
## So What are the Problems?
103+
104+
1. Problem with the constructor function: Every object has its own instance of the function
105+
2. Problem with the prototype: Modifying a property using one object reflects the other object also
106+
107+
To solve both problems, we can define all the object-specific properties inside the constructor and all shared properties and methods inside the prototype as shown below:
108+
109+
```javascript
110+
//Define the object specific properties inside the constructor
111+
function Human(name, age) {
112+
(this.name = name), (this.age = age), (this.friends = ['Jadeja', 'Vijay']);
113+
}
114+
//Define the shared properties and methods using the prototype
115+
Human.prototype.sayName = function () {
116+
console.log(this.name);
117+
};
118+
//Create two objects using the Human constructor function
119+
var person1 = new Human('Virat', 31);
120+
var person2 = new Human('Sachin', 40);
121+
122+
//Lets check if person1 and person2 have points to the same instance of the sayName function
123+
console.log(person1.sayName === person2.sayName); // true
124+
125+
//Let's modify friends property and check
126+
person1.friends.push('Amit');
127+
128+
console.log(person1.friends); // Output: "Jadeja, Vijay, Amit"
129+
console.log(person2.friends); //Output: "Jadeja, Vijay"
130+
```
28131

132+
![](../images/protoSummary.jpg)
17.6 KB
Loading
54.6 KB
Loading

session 1/images/protoSummary.jpg

95.3 KB
Loading

session 1/images/sharedProto.jpg

90.5 KB
Loading

session 1/server/index.js

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
1-
var log = console.log;
2-
3-
log('Inside global execution context');
4-
5-
function functionOne() {
6-
log('Inside function one');
7-
8-
function setTimeoutFunction() {
9-
log('Inside setTimeoutFunction: I will be executed atleast after 1 sec');
10-
}
11-
12-
setTimeout(setTimeoutFunction, 1000);
13-
14-
for (var i = 0; i < 100000000; i++) {
15-
// Blocking code. This makes the for loop to execute for more than 1 second
16-
// Still setTimeoutFunction is not executed. It gets executed only after
17-
// last statement of the code
18-
}
19-
20-
log('Exiting functionOne');
21-
}
22-
23-
functionOne();
24-
25-
log('Exiting global execution context');
1+
//Create an empty constructor function
2+
function Person() {}
3+
//Add property name, age to the prototype property of the Person constructor function
4+
Person.prototype.name = 'Ashwin';
5+
Person.prototype.age = 26;
6+
Person.prototype.friends = ['Jadeja', 'Vijay']; //Arrays are of reference type in JavaScript
7+
Person.prototype.sayName = function () {
8+
console.log(this.name);
9+
};
10+
11+
//Create objects using the Person constructor function
12+
var person1 = new Person();
13+
var person2 = new Person();
14+
15+
//Add a new element to the friends array
16+
person1.friends.push('Amit');
17+
18+
console.log(person1.friends); // Output: "Jadeja, Vijay, Amit"
19+
console.log(person2.friends); // Output: "Jadeja, Vijay, Amit"
20+
21+
person1.friends = ['akash'];
22+
23+
console.log(person1.friends); // Output: "Jadeja, Vijay, Amit"
24+
console.log(person2.friends); // Output: "Jadeja, Vijay, Amit"

0 commit comments

Comments
 (0)