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
+140-1Lines changed: 140 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -1138,6 +1138,145 @@ But it fails, as shown by the last statement in that snippet. Beware that gotcha
1138
1138
1139
1139
OK, we've laid out a bunch of disparate class features. I want to wrap up this chapter by trying to illustrate a sampling of these capabilities in a single example that's a little less basic/contrived.
Take some time to read and digest those `class` definitions. Note which of the `class` features from this chapter that you see being used.
1249
+
1250
+
| NOTE: |
1251
+
| :--- |
1252
+
| 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! |
1253
+
1254
+
Let's now see these three classes in use:
1255
+
1256
+
```js
1257
+
var callParents =newReminder(
1258
+
"Call parents to say hi",
1259
+
newDate("July 7, 2022 11:00:00 UTC")
1260
+
);
1261
+
callParents.summary();
1262
+
// (586380912) Call parents to say hi at Thu,
1263
+
// 07 Jul 2022 11:00:00 GMT
1264
+
1265
+
callParents.markComplete();
1266
+
callParents.summary();
1267
+
// (586380912) Complete.
1268
+
1269
+
var interview =newMeeting(
1270
+
"Job Interview: ABC Tech",
1271
+
newDate("June 23, 2022 08:30:00 UTC"),
1272
+
newDate("June 23, 2022 09:15:00 UTC")
1273
+
);
1274
+
interview.summary();
1275
+
// (994337604) Job Interview: ABC Tech at Thu,
1276
+
// 23 Jun 2022 08:30:00 GMT - Thu, 23 Jun 2022
1277
+
// 09:15:00 GMT
1278
+
```
1279
+
1280
+
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.
1142
1281
1143
1282
[^POLP]: *Principle of Least Privilege*, https://en.wikipedia.org/wiki/Principle_of_least_privilege, 15 July 2022.
0 commit comments