From 01b2cc1bc543753dc801944bfa27c93e90819cdf Mon Sep 17 00:00:00 2001 From: Kyle Simpson Date: Sat, 16 Jul 2022 08:48:07 -0500 Subject: [PATCH] objects-classes, ch3: adding a bit more to the final full 'class' example --- objects-classes/ch3.md | 49 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 7 deletions(-) diff --git a/objects-classes/ch3.md b/objects-classes/ch3.md index 005395ed4..8b374b6b7 100644 --- a/objects-classes/ch3.md +++ b/objects-classes/ch3.md @@ -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; @@ -1185,7 +1194,7 @@ class CalendarItem { this.#ID = id; } else { - throw new Error("ID is already set"); + throw new Error(CalendarItem.ERROR_100); } } @@ -1198,7 +1207,7 @@ class CalendarItem { this.#setID(id); } else { - throw new Error("Don't instantiate 'CalendarItem' directly."); + throw new Error(CalendarItem.ERROR_300); } } getID() { @@ -1206,7 +1215,7 @@ class CalendarItem { return this.#ID; } else { - throw new Error("ID is unset"); + throw new Error(CalendarItem.ERROR_200); } } getDateTimeStr() { @@ -1228,6 +1237,7 @@ class CalendarItem { class Reminder extends CalendarItem { #complete = false + [Symbol.toStringTag] = "Reminder" constructor(description,startDateTime) { super(); @@ -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(); @@ -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: | | :--- | @@ -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.