Skip to content

Commit 01b2cc1

Browse files
committedJul 16, 2022
objects-classes, ch3: adding a bit more to the final full 'class' example
1 parent ebac5cc commit 01b2cc1

File tree

1 file changed

+42
-7
lines changed

1 file changed

+42
-7
lines changed
 

‎objects-classes/ch3.md

+42-7
Original file line numberDiff line numberDiff line change
@@ -1170,6 +1170,15 @@ class CalendarItem {
11701170
static #isUnset(v) {
11711171
return v === this.#UNSET;
11721172
}
1173+
static {
1174+
for (let [idx,msg] of [
1175+
"ID is already set.",
1176+
"ID is unset.",
1177+
"Don't instantiate 'CalendarItem' directly.",
1178+
].entries()) {
1179+
this[`ERROR_${(idx+1)*100}`] = msg;
1180+
}
1181+
}
11731182
static isSameItem(item1,item2) {
11741183
if (#ID in item1 && #ID in item2) {
11751184
return item1.#ID === item2.#ID;
@@ -1185,7 +1194,7 @@ class CalendarItem {
11851194
this.#ID = id;
11861195
}
11871196
else {
1188-
throw new Error("ID is already set");
1197+
throw new Error(CalendarItem.ERROR_100);
11891198
}
11901199
}
11911200

@@ -1198,15 +1207,15 @@ class CalendarItem {
11981207
this.#setID(id);
11991208
}
12001209
else {
1201-
throw new Error("Don't instantiate 'CalendarItem' directly.");
1210+
throw new Error(CalendarItem.ERROR_300);
12021211
}
12031212
}
12041213
getID() {
12051214
if (!CalendarItem.#isUnset(this.#ID)) {
12061215
return this.#ID;
12071216
}
12081217
else {
1209-
throw new Error("ID is unset");
1218+
throw new Error(CalendarItem.ERROR_200);
12101219
}
12111220
}
12121221
getDateTimeStr() {
@@ -1228,6 +1237,7 @@ class CalendarItem {
12281237
class Reminder extends CalendarItem {
12291238
#complete = false
12301239

1240+
[Symbol.toStringTag] = "Reminder"
12311241
constructor(description,startDateTime) {
12321242
super();
12331243

@@ -1251,14 +1261,15 @@ class Reminder extends CalendarItem {
12511261
}
12521262

12531263
class Meeting extends CalendarItem {
1254-
endDateTime = null
1255-
12561264
#getEndDateTimeStr() {
12571265
if (this.endDateTime instanceof Date) {
12581266
return this.endDateTime.toUTCString();
12591267
}
12601268
}
12611269

1270+
endDateTime = null
1271+
1272+
[Symbol.toStringTag] = "Meeting"
12621273
constructor(description,startDateTime,endDateTime) {
12631274
super();
12641275

@@ -1276,7 +1287,7 @@ class Meeting extends CalendarItem {
12761287
}
12771288
```
12781289

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.
1290+
Take some time to read and digest those `class` definitions. Did you spot most of the `class` features we talked about in this chapter?
12801291

12811292
| NOTE: |
12821293
| :--- |
@@ -1289,25 +1300,49 @@ var callMyParents = new Reminder(
12891300
"Call my parents to say hi",
12901301
new Date("July 7, 2022 11:00:00 UTC")
12911302
);
1303+
callMyParents.toString();
1304+
// [object Reminder]
12921305
callMyParents.summary();
12931306
// (586380912) Call my parents to say hi at
12941307
// Thu, 07 Jul 2022 11:00:00 GMT
1295-
12961308
callMyParents.markComplete();
12971309
callMyParents.summary();
12981310
// (586380912) Complete.
1311+
callMyParents instanceof Reminder;
1312+
// true
1313+
callMyParents instanceof CalendarItem;
1314+
// true
1315+
callMyParents instanceof Meeting;
1316+
// false
1317+
12991318

13001319
var interview = new Meeting(
13011320
"Job Interview: ABC Tech",
13021321
new Date("June 23, 2022 08:30:00 UTC"),
13031322
new Date("June 23, 2022 09:15:00 UTC")
13041323
);
1324+
interview.toString();
1325+
// [object Meeting]
13051326
interview.summary();
13061327
// (994337604) Job Interview: ABC Tech at Thu,
13071328
// 23 Jun 2022 08:30:00 GMT - Thu, 23 Jun 2022
13081329
// 09:15:00 GMT
1330+
interview instanceof Meeting;
1331+
// true
1332+
interview instanceof CalendarItem;
1333+
// true
1334+
interview instanceof Reminder;
1335+
// false
1336+
1337+
1338+
Reminder.isSameItem(callMyParents,callMyParents);
1339+
// true
1340+
Meeting.isSameItem(callMyParents,interview);
1341+
// false
13091342
```
13101343

1344+
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.
1345+
13111346
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.
13121347

13131348
[^POLP]: *Principle of Least Privilege*, https://en.wikipedia.org/wiki/Principle_of_least_privilege, 15 July 2022.

0 commit comments

Comments
 (0)
Failed to load comments.