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
+40-3Lines changed: 40 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -270,7 +270,7 @@ class Point3d {
270
270
271
271
| TIP: |
272
272
| :--- |
273
-
| You can think of public field declarations as if they appear at the top of the constructor, each prefixed with an implied `this.` that you get to omit in the declarative `class` body form. |
273
+
| You can mostly think of public field declarations as if they appear at the top of the `constructor(..)`, each prefixed with an implied `this.` that you get to omit in the declarative `class` body form. But, there's a catch! See "That's Super!" later for more information about it. |
274
274
275
275
Just like computed property names (see Chapter 1), field names can be computed:
276
276
@@ -486,7 +486,7 @@ The ability for methods of the same name, at different levels of the inheritance
486
486
487
487
### That's Super!
488
488
489
-
In addition to a subclass method accessing an inherited method definition (even if overriden on the subclass) via `super.` reference, a subclass constructor can manually invoke the inherited base class constructor via `super(..)` function invocation:
489
+
In addition to a subclass method accessing an inherited method definition (even if overriden on the subclass) via `super.` reference, a subclass constructor must manually invoke the inherited base class constructor via `super(..)` function invocation:
490
490
491
491
```js
492
492
classPoint2d {
@@ -516,7 +516,44 @@ point.toString(); // (3,4,5)
516
516
517
517
| WARNING: |
518
518
| :--- |
519
-
| An explicitly defined subclass constructor *must* call `super(..)` to run the inherited class's initialization, and that must occur before the subclass constructor makes any references to `this` or finishes/returns. Otherwise, a runtime exception will be thrown when that subclass constructor is invoked (via `new`). If you omit the subclass constructor, the default constructor automatically thankfully invokes `super()` for you. |
519
+
| An explicitly defined subclass constructor *must* call `super(..)` to run the inherited class's initialization, and that must occur before the subclass constructor makes any references to `this` or finishes/returns. Otherwise, a runtime exception will be thrown when that subclass constructor is invoked (via `new`). If you omit the subclass constructor, the default constructor automatically -- thankfully! -- invokes `super()` for you. |
520
+
521
+
One nuance to be aware of: if you define a field (public or private) inside a subclass, and explicitly define a `constructor(..)` for this subclass, the field initializations will be processed not at the top of the constructor, but *between* the `super(..)` call and any subsequent code in the constructor.
522
+
523
+
Pay close attention to the order of console messages here:
524
+
525
+
```js
526
+
classPoint2d {
527
+
x
528
+
y
529
+
constructor(x,y) {
530
+
console.log("Running Point2d(..) constructor");
531
+
this.x= x;
532
+
this.y= y;
533
+
}
534
+
}
535
+
536
+
classPoint3dextendsPoint2d {
537
+
z =console.log("Initializing field 'z'")
538
+
539
+
constructor(x,y,z) {
540
+
console.log("Running Point3d(..) constructor");
541
+
super(x,y);
542
+
543
+
console.log(`Setting instance property 'z' to ${z}`);
0 commit comments