Skip to content

Commit

Permalink
objects-classes, ch3: adding a bit more to the final full 'class' exa…
Browse files Browse the repository at this point in the history
…mple
  • Loading branch information
getify committed Jul 16, 2022
1 parent ebac5cc commit 01b2cc1
Showing 1 changed file with 42 additions and 7 deletions.
49 changes: 42 additions & 7 deletions objects-classes/ch3.md
Original file line number Diff line number Diff line change
Expand Up @@ -1170,6 +1170,15 @@ class CalendarItem {
static #isUnset(v) {
return v === this.#UNSET;
}
static {
for (let [idx,msg] of [
"ID is already set.",
"ID is unset.",
"Don't instantiate 'CalendarItem' directly.",
].entries()) {
this[`ERROR_${(idx+1)*100}`] = msg;
}
}
static isSameItem(item1,item2) {
if (#ID in item1 && #ID in item2) {
return item1.#ID === item2.#ID;
Expand All @@ -1185,7 +1194,7 @@ class CalendarItem {
this.#ID = id;
}
else {
throw new Error("ID is already set");
throw new Error(CalendarItem.ERROR_100);
}
}

Expand All @@ -1198,15 +1207,15 @@ class CalendarItem {
this.#setID(id);
}
else {
throw new Error("Don't instantiate 'CalendarItem' directly.");
throw new Error(CalendarItem.ERROR_300);
}
}
getID() {
if (!CalendarItem.#isUnset(this.#ID)) {
return this.#ID;
}
else {
throw new Error("ID is unset");
throw new Error(CalendarItem.ERROR_200);
}
}
getDateTimeStr() {
Expand All @@ -1228,6 +1237,7 @@ class CalendarItem {
class Reminder extends CalendarItem {
#complete = false

[Symbol.toStringTag] = "Reminder"
constructor(description,startDateTime) {
super();

Expand All @@ -1251,14 +1261,15 @@ class Reminder extends CalendarItem {
}

class Meeting extends CalendarItem {
endDateTime = null

#getEndDateTimeStr() {
if (this.endDateTime instanceof Date) {
return this.endDateTime.toUTCString();
}
}

endDateTime = null

[Symbol.toStringTag] = "Meeting"
constructor(description,startDateTime,endDateTime) {
super();

Expand All @@ -1276,7 +1287,7 @@ class Meeting extends CalendarItem {
}
```

Take some time to read and digest those `class` definitions. Note which of the `class` features from this chapter that you see being used.
Take some time to read and digest those `class` definitions. Did you spot most of the `class` features we talked about in this chapter?

| NOTE: |
| :--- |
Expand All @@ -1289,25 +1300,49 @@ var callMyParents = new Reminder(
"Call my parents to say hi",
new Date("July 7, 2022 11:00:00 UTC")
);
callMyParents.toString();
// [object Reminder]
callMyParents.summary();
// (586380912) Call my parents to say hi at
// Thu, 07 Jul 2022 11:00:00 GMT

callMyParents.markComplete();
callMyParents.summary();
// (586380912) Complete.
callMyParents instanceof Reminder;
// true
callMyParents instanceof CalendarItem;
// true
callMyParents instanceof Meeting;
// false


var interview = new Meeting(
"Job Interview: ABC Tech",
new Date("June 23, 2022 08:30:00 UTC"),
new Date("June 23, 2022 09:15:00 UTC")
);
interview.toString();
// [object Meeting]
interview.summary();
// (994337604) Job Interview: ABC Tech at Thu,
// 23 Jun 2022 08:30:00 GMT - Thu, 23 Jun 2022
// 09:15:00 GMT
interview instanceof Meeting;
// true
interview instanceof CalendarItem;
// true
interview instanceof Reminder;
// false


Reminder.isSameItem(callMyParents,callMyParents);
// true
Meeting.isSameItem(callMyParents,interview);
// false
```

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.

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.

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

0 comments on commit 01b2cc1

Please sign in to comment.