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
Fun extra step: If you get bored, template them and make a page by rendering the results in HTML :smile: with something like `document.querySelector('.move').innerHTML = ...`
Copy file name to clipboardExpand all lines: fundamentals/oop_classes.md
+32-3Lines changed: 32 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -214,10 +214,38 @@ months
214
214
```
215
215
216
216
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
-
219
217

220
218
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
+
221
249
### ES6 Classes
222
250
223
251
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) {
0 commit comments