You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: session 1/chapters/3. Prototypes.md
+112-8Lines changed: 112 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff 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
-
9
1
# Problem with creating objects with the constructor function:
10
2
11
3
Consider the constructor function below:
@@ -25,4 +17,116 @@ var person2 = new Human('Sachin', 'Tendulkar');
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`.
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.
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
0 commit comments