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
+29-3
Original file line number
Diff line number
Diff line change
@@ -1099,7 +1099,7 @@ The `#printError()` static private function here has a `this`, but that's refere
1099
1099
1100
1100
Remember: private statics are similarly not-inherited by subclasses just as private members/methods are not.
1101
1101
1102
-
#### Subclassing Gotcha
1102
+
#### Gotcha: Subclassing With Static Privates and `this`
1103
1103
1104
1104
Recall that inherited methods, invoked from a subclass, have no trouble accessing (via `this.#whatever` style references) any privates from their own base class:
1105
1105
@@ -1129,7 +1129,7 @@ point.printID();
1129
1129
1130
1130
That works just fine.
1131
1131
1132
-
Unfortunately, and (to me) a little unexpectedly/inconsistently, the same is not true of private statics accessed from inherited public static functions:
1132
+
Unfortunately, and (to me) quite unexpectedly/inconsistently, the same is not true of private statics accessed from inherited public static functions:
1133
1133
1134
1134
```js
1135
1135
classPoint2d {
@@ -1158,7 +1158,33 @@ Point3d.printError();
1158
1158
1159
1159
The `printError()` static is inherited (shared via `[[Prototype]]`) from `Point2d` to `Point3d` just fine, which is why the function references are identical. Like the non-static snippet just above, you might have expected the `Point3d.printError()` static invocation to resolve via the `[[Prototype]]` chain to its original base class (`Point2d`) location, thereby letting it access the base class's `#errorMsg` static private.
1160
1160
1161
-
But it fails, as shown by the last statement in that snippet. Beware that gotcha!
1161
+
But it fails, as shown by the last statement in that snippet. The reason it fails here, but not with the previous snippet, is a convoluted brain twister. I'm not going to dig into the *why* explanation here, frankly because it boils my blood to do so.
1162
+
1163
+
There's a *fix*, though. In the static function, instead of `this.#errorMsg`, swap that for `Point2d.#errorMsg`, and now it works:
1164
+
1165
+
```js
1166
+
classPoint2d {
1167
+
static #errorMsg ="Out of bounds."
1168
+
staticprintError() {
1169
+
// the fixed reference vvvvvv
1170
+
console.log(`Error: ${Point2d.#errorMsg}`);
1171
+
}
1172
+
1173
+
// ..
1174
+
}
1175
+
1176
+
classPoint3dextendsPoint2d {
1177
+
// ..
1178
+
}
1179
+
1180
+
Point2d.printError();
1181
+
// Error: Out of bounds.
1182
+
1183
+
Point3d.printError();
1184
+
// Error: Out of bounds. <-- phew, it works now!
1185
+
```
1186
+
1187
+
If public static functions are being inherited, use the class name to access any private statics instead of using `this.` references. Beware that gotcha!
0 commit comments