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
@@ -1276,11 +1290,11 @@ class Meeting extends CalendarItem {
1276
1290
}
1277
1291
```
1278
1292
1279
-
Take some time to read and digest those `class` definitions. Note which of the `class` features from this chapter that you see being used.
1293
+
Take some time to read and digest those `class` definitions. Did you spot most of the `class` features we talked about in this chapter?
1280
1294
1281
1295
| NOTE: |
1282
1296
| :--- |
1283
-
| One question you may have: why didn't I move the common logic of `description` and `startDateTime` setting from both subclass constructors into the single base constructor. This is a nuanced point, but it's not my intention that `CalendarItem` ever be directly instantiated; it's what in class-oriented terms we refer to as an "abstract class". That's why I'm using `new.target` to throw an error if the `CalendarItem` class is ever directly instantiated! |
1297
+
| One question you may have: why didn't I move the repeated logic of `description` and `startDateTime` setting from both subclass constructors into the single base constructor? This is a nuanced point, but it's not my intention that `CalendarItem` ever be directly instantiated; it's what in class-oriented terms we refer to as an "abstract class". That's why I'm using `new.target` to throw an error if the `CalendarItem` class is ever directly instantiated! So I don't want to imply by signature that the `CalendarItem(..)` constructor should ever be directly used.|
1284
1298
1285
1299
Let's now see these three classes in use:
1286
1300
@@ -1289,25 +1303,49 @@ var callMyParents = new Reminder(
1289
1303
"Call my parents to say hi",
1290
1304
newDate("July 7, 2022 11:00:00 UTC")
1291
1305
);
1306
+
callMyParents.toString();
1307
+
// [object Reminder]
1292
1308
callMyParents.summary();
1293
1309
// (586380912) Call my parents to say hi at
1294
1310
// Thu, 07 Jul 2022 11:00:00 GMT
1295
-
1296
1311
callMyParents.markComplete();
1297
1312
callMyParents.summary();
1298
1313
// (586380912) Complete.
1314
+
callMyParents instanceof Reminder;
1315
+
// true
1316
+
callMyParents instanceof CalendarItem;
1317
+
// true
1318
+
callMyParents instanceof Meeting;
1319
+
// false
1320
+
1299
1321
1300
1322
var interview =newMeeting(
1301
1323
"Job Interview: ABC Tech",
1302
1324
newDate("June 23, 2022 08:30:00 UTC"),
1303
1325
newDate("June 23, 2022 09:15:00 UTC")
1304
1326
);
1327
+
interview.toString();
1328
+
// [object Meeting]
1305
1329
interview.summary();
1306
1330
// (994337604) Job Interview: ABC Tech at Thu,
1307
1331
// 23 Jun 2022 08:30:00 GMT - Thu, 23 Jun 2022
1308
1332
// 09:15:00 GMT
1333
+
interview instanceof Meeting;
1334
+
// true
1335
+
interview instanceof CalendarItem;
1336
+
// true
1337
+
interview instanceof Reminder;
1338
+
// false
1339
+
1340
+
1341
+
Reminder.isSameItem(callMyParents,callMyParents);
1342
+
// true
1343
+
Meeting.isSameItem(callMyParents,interview);
1344
+
// false
1309
1345
```
1310
1346
1347
+
Admittedly, some bits of this example are a little contrived. But honestly, I think pretty much all of this is plausible and reasonable usages of the various `class` features.
1348
+
1311
1349
By the way, there's probably a million different ways to structure the above code logic. I'm by no means claiming this is the *right* or *best* way to do so. As an exercise for the reader, try your hand and writing it yourself, and take note of things you did differently than my approach.
1312
1350
1313
1351
[^POLP]: *Principle of Least Privilege*, https://en.wikipedia.org/wiki/Principle_of_least_privilege, 15 July 2022.
0 commit comments