After reading the first two articles, "Prototypes" and "Inheritance," it's time to practice what we've learned. The example I came up with for this article is for learning purposes only. It's not related to real-world use cases.
Coming up with a good example turned out to be tricky. One reason is the difficulty of coming up with an example with meaningful logic and hierarchy. Hopefully, my solution will do the job.
The idea is as follows:
- We need to create task items of some kind (like to-do lists). They could represent "li" elements, for example.
- Now, let's assume there are two types of these items based on priority: primary and secondary.
- Further, let's assume that all task items have the same parent element (ul) and CSS class. One way to provide this information to every task instance is through prototypal inheritance. Additionally, if we need to change the parent element or class name, every task item will detect the change.