The Command pattern turns function calls into first-class objects. This makes operations serializable, loggable, and reversible. Redux actions are a functional variant of this pattern.
Trade-offs:
More boilerplate than a direct function call
Memory cost for storing command history
Undo logic can be complex for operations with side effects
When NOT to use:
When undo/redo is not needed and operations are one-shot
For simple event handlers — a plain callback is sufficient
Read the instructions and examples in this document.
Apply the patterns to your implementation, adapting to your specific context.
Verify your implementation against the details and edge cases listed above.
Harness Integration
Type: knowledge — this skill is a reference document, not a procedural workflow.
No tools or state — consumed as context by other skills and agents.
Success Criteria
The patterns described in this document are applied correctly in the implementation.
Edge cases and anti-patterns listed in this document are avoided.
1---2name: js-command-pattern3description: JS Command Pattern4---5# JS Command Pattern67> Encapsulate operations as objects to support undo, queue, and logging89## When to Use1011- You need undo/redo functionality12- Operations should be queued, deferred, or logged13- You want to decouple the sender of an operation from its executor1415## Instructions16171. Define a command interface with `execute()` and optionally `undo()` methods.182. Each command class captures the receiver and parameters needed to perform (and reverse) the operation.193. Store executed commands in a history stack for undo support.204. The invoker (button, menu, keyboard shortcut) calls `command.execute()` without knowing the implementation.2122```javascript23class AddTextCommand {24 constructor(editor, text) {25 this.editor = editor;26 this.text = text;27 this.prevContent = null;28 }2930 execute() {31 this.prevContent = this.editor.content;32 this.editor.content += this.text;33 }3435 undo() {36 this.editor.content = this.prevContent;37 }38}3940const editor = { content: 'Hello' };41const history = [];4243const cmd = new AddTextCommand(editor, ' World');44cmd.execute();45history.push(cmd);46console.log(editor.content); // 'Hello World'4748history.pop().undo();49console.log(editor.content); // 'Hello'50```5152## Details5354The Command pattern turns function calls into first-class objects. This makes operations serializable, loggable, and reversible. Redux actions are a functional variant of this pattern.5556**Trade-offs:**5758- More boilerplate than a direct function call59- Memory cost for storing command history60- Undo logic can be complex for operations with side effects6162**When NOT to use:**6364- When undo/redo is not needed and operations are one-shot65- For simple event handlers — a plain callback is sufficient6667## Source6869https://patterns.dev/javascript/command-pattern7071## Process72731. Read the instructions and examples in this document.742. Apply the patterns to your implementation, adapting to your specific context.753. Verify your implementation against the details and edge cases listed above.7677## Harness Integration7879- **Type:** knowledge — this skill is a reference document, not a procedural workflow.80- **No tools or state** — consumed as context by other skills and agents.8182## Success Criteria8384- The patterns described in this document are applied correctly in the implementation.85- Edge cases and anti-patterns listed in this document are avoided.
Run npx skillmds@latest add intense-visions/js-command-pattern in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
JS Command Pattern It is listed under Coding & Dev Tools on SkillMD.
This skill has not completed SkillMD's automated safety review yet. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
Intense-Visions (@intense-visions) published this skill. Their other Agent Skills are listed on their SkillMD profile.