What It Is
The Prototype pattern creates new objects by cloning an existing one instead of building from zero. You prepare one object as a template, then copy it and change only what differs.
The one thing to get right: a plain copy is shallow. Spreading an object, { ...doc }, copies the reference to the tags array, so the copy and the original still share one array — push to one and the other changes too. A proper clone() method copies the array as well, so the clone is truly independent.
Example: A Document Template
The template carries the parts every document repeats. clone() copies it — including the tags array — so editing the copy leaves the template untouched.
Clone, then change the difference
The report got a new title and an extra tag, and the template still has only [ 'draft' ] — because clone() copied the array with [...tags] instead of sharing it. For plain data with no methods, the built-in structuredClone() does the same deep copy in one call.
Output: TypeScript file and captured output
class TextDocument {
title: string
footer: string
tags: string[]
constructor(title: string, footer: string, tags: string[]) {
this.title = title
this.footer = footer
this.tags = [...tags]
}
// clone copies the document — including the array inside it.
clone(): TextDocument {
return new TextDocument(this.title, this.footer, this.tags)
}
}
const template = new TextDocument("Untitled", "PromptCookbook 2026", ["draft"])
const report = template.clone()
report.title = "Yearly Report"
report.tags.push("finance")
console.log("template:", template.title, template.tags)
console.log("clone: ", report.title, report.tags)
When to Use It
Use it when new objects are mostly copies of a prepared one — documents from a template, game entities from a spawner, test data from a fixture.
Skip it when objects are cheap to build fresh, or hold nothing but plain values. For a flat object of strings and numbers, a spread copy { ...doc } is already a full copy.
Pros and Cons
Pros: cheap creation of preconfigured objects; the template documents the defaults; no repeated setup code.
Cons: deep copying is easy to get subtly wrong — a forgotten array or nested object quietly links clone and original; clones drift from the template silently over time.