Summary
The PROTOTYPE pattern lets a client create new objects by copying an example. A major difference between calling a constructor and copying an object is that a copy typically includes some of the state of the original object. You can use this to your advantage, particularly when different “classes” of objects differ only in their attributes, not in their behaviors. In such a case, you can create new classes at runtime by crafting prototypical objects for a client to copy.
When you need to create a copy, Object.clone() can help, but you must remember that this method creates a new object with the same fields. This object may not be a suitable clone, but any difficulties related to deep copying are your responsibility. If creating a deep ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access