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: objects-classes/ch3.md
+7-5
Original file line number
Diff line number
Diff line change
@@ -196,7 +196,7 @@ point.setX(3);
196
196
197
197
The `setX` property (method) *looks like* it exists on (is owned by) the `point` object here. But that's a mirage. Each class method is added to the `prototype`object, a property of the constructor function.
198
198
199
-
So, `setX(..)` only exists as `Point2d.prototype.greeting`. Since `point` is `[[Prototype]]` linked to `Point2d.prototype` (see Chapter 2) via the `new` keyword instantiation, the `point.setX(..)` reference traverses the `[[Prototype]]` chain and finds the method to execute.
199
+
So, `setX(..)` only exists as `Point2d.prototype.setX`. Since `point` is `[[Prototype]]` linked to `Point2d.prototype` (see Chapter 2) via the `new` keyword instantiation, the `point.setX(..)` reference traverses the `[[Prototype]]` chain and finds the method to execute.
200
200
201
201
Class methods should only be invoked via an instance; `Point2d.setX(..)` doesn't work because there *is no* such property. You *could* invoke `Point2d.prototype.setX(..)`, but that's not generally proper/advised in standard class-oriented coding. Always access class methods via the instances.
202
202
@@ -237,7 +237,7 @@ Instead of defining a class instance member imperatively via `this.` in the cons
237
237
238
238
```js
239
239
classPoint2d {
240
-
//this is a public field
240
+
//these are public fields
241
241
x =0
242
242
y =0
243
243
@@ -450,7 +450,7 @@ var point = new Point3d();
450
450
point.printX(); // double x: 42
451
451
```
452
452
453
-
The `Point3d` subclass overrides the inherited `getX()` method to give it different behavior. However, you can still instantiate the base `Point2d` class, which would then give an object that uses the original (`"That's ..."`) definition for `greeting()`.
453
+
The `Point3d` subclass overrides the inherited `getX()` method to give it different behavior. However, you can still instantiate the base `Point2d` class, which would then give an object that uses the original (`return this.x;`) definition for `getX()`.
454
454
455
455
If you want to access an inherited method from a subclass even if it's been overriden, you can use `super` instead of `this`:
456
456
@@ -482,7 +482,7 @@ var point = new Point3d();
482
482
point.printX(); // x: 21
483
483
```
484
484
485
-
The ability for methods of the same name, at different levels of the inheritance hierarchy, to exhibit different behavior when either accessed directly, or relatively with `super`, is called *polymorphism*. It's a very powerful part of class-orientation, when used appropriately.
485
+
The ability for methods of the same name, at different levels of the inheritance hierarchy, to exhibit different behavior when either accessed directly, or relatively with `super`, is called *method polymorphism*. It's a very powerful part of class-orientation, when used appropriately.
486
486
487
487
### That's Super!
488
488
@@ -555,6 +555,8 @@ var point = new Point3d(3,4,5);
555
555
// Setting instance property 'z' to 5
556
556
```
557
557
558
+
As the console messages illustrate, the `z = ..` field initialization happens *immediately after* the `super(x,y)` call, *before* the ``console.log(`Setting instance...`)`` is executed. Perhaps think of it like the field initializations attached to the end of the `super(..)` call, so they run before anything else in the constructor does.
559
+
558
560
#### Which Class?
559
561
560
562
You may need to determine in a constructor if that class is being instantiated directly, or being instantiated from a subclass with a `super()` call. We can use a special "pseudo property" `new.target`:
@@ -700,7 +702,7 @@ Where is that `toString()` method located? On the prototype object:
And `b` has access to that method via its `[[Prototype]]` linkage (see Chapter 2). In other words, the prototype objects **share access** to their method(s) with the subclass(es) and instance(s). The method(s) stay in place, and are not copied down the inheritance chain.
705
+
And `anotherPoint` has access to that method via its `[[Prototype]]` linkage (see Chapter 2). In other words, the prototype objects **share access** to their method(s) with the subclass(es) and instance(s). The method(s) stay in place, and are not copied down the inheritance chain.
704
706
705
707
As nice as the `class` syntax is, don't forget what's really happening under the syntax: JS is *just* wiring up objects to each other along a `[[Prototype]]` chain.
0 commit comments