Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 04d9f20

Browse files
committed
added prototype chain explanation
1 parent a77ec6d commit 04d9f20

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

fundamentals/oop_classes.md

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,38 @@ months
214214
```
215215

216216

217-
The diagram below depicts how this sharing works out. At this time it is not necessary that you understand every detail. Just note how there is a single copy of functions, shared by all instances of the `Months` objects.
218-
219217
![prototype](assets/prototype.png)
220218

219+
### Prototype vs __prototype
220+
221+
The above diagram depicts how this sharing works out.
222+
223+
The `prototype` property exists on all functions but is only relevant when that function is used as a **constructor function**. By assigning methods to the `prototype` property you are basically defining a ‘prototype’ object that will be shared by all objects created through the constructor function when called in conjunction with the `new` keyword.
224+
225+
In contrast to `prototype`, the `__proto__` property (in documentation sometimes denoted as `[[proto]]`) is a property that exist on objects created through the constructor function. This `__proto__` property points to the shared ‘prototype’ object, as defined on the constructor function’s `prototype` property.
226+
227+
The prototype object itself also has a `__proto__` property. In most cases this property points to the prototype of the standard JavaScript `Object` prototype. This is because, ultimately, all objects in JavaScript are prototype-linked to the `Object` prototype. In OOP terms one would say that all JavaScript objects ultimately derive from `Object`.
228+
229+
The `__proto__` property of the `Object` prototype itself has the value `null`. This signals the end of the prototype chain.
230+
231+
When you call a method on an object that does not exist on the object itself, the JavaScript engine will 'walk' down the prototype chain until it finds the requested method _or_ until it reaches the end of the chain.
232+
233+
If the method is found, JavaScript calls the method, setting its `this` value to the object the method was called on. This happens behind the scenes without requiring intervention from the programmer.
234+
235+
If the method was not found by walking the prototype chain, a run-time error is produced, e.g:
236+
237+
```js
238+
myObj.someNonExistingMethod();
239+
240+
```
241+
242+
```
243+
myObj.someNonExistingMethod();
244+
^
245+
246+
TypeError: myObj.someNonExistingMethod is not a function
247+
```
248+
221249
### ES6 Classes
222250

223251
In ES6 a new way of defining objects and its methods was introduced. It uses the same `prototype` mechanism behind the scenes, but its syntax is closer to that of other object-oriented languages, such as Java, etc. Because it is only new syntax, hiding the intricacies of the `prototype`, it is often designated as 'syntactic sugaring'.
@@ -312,4 +340,5 @@ Array.prototype.myFilter = function (callback) {
312340
}
313341
return arr;
314342
};
315-
```
343+
```
344+

0 commit comments

Comments
 (0)