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 Pattern4metadata:5 source: https://skillmd.com/skills/intense-visions/js-command-pattern6---7# JS Command Pattern89> Encapsulate operations as objects to support undo, queue, and logging1011## When to Use1213- You need undo/redo functionality14- Operations should be queued, deferred, or logged15- You want to decouple the sender of an operation from its executor1617## Instructions18191. Define a command interface with `execute()` and optionally `undo()` methods.202. Each command class captures the receiver and parameters needed to perform (and reverse) the operation.213. Store executed commands in a history stack for undo support.224. The invoker (button, menu, keyboard shortcut) calls `command.execute()` without knowing the implementation.2324```javascript25class AddTextCommand {26 constructor(editor, text) {27 this.editor = editor;28 this.text = text;29 this.prevContent = null;30 }3132 execute() {33 this.prevContent = this.editor.content;34 this.editor.content += this.text;35 }3637 undo() {38 this.editor.content = this.prevContent;39 }40}4142const editor = { content: 'Hello' };43const history = [];4445const cmd = new AddTextCommand(editor, ' World');46cmd.execute();47history.push(cmd);48console.log(editor.content); // 'Hello World'4950history.pop().undo();51console.log(editor.content); // 'Hello'52```5354## Details5556The 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.5758**Trade-offs:**5960- More boilerplate than a direct function call61- Memory cost for storing command history62- Undo logic can be complex for operations with side effects6364**When NOT to use:**6566- When undo/redo is not needed and operations are one-shot67- For simple event handlers — a plain callback is sufficient6869## Source7071https://patterns.dev/javascript/command-pattern7273## Process74751. Read the instructions and examples in this document.762. Apply the patterns to your implementation, adapting to your specific context.773. Verify your implementation against the details and edge cases listed above.7879## Harness Integration8081- **Type:** knowledge — this skill is a reference document, not a procedural workflow.82- **No tools or state** — consumed as context by other skills and agents.8384## Success Criteria8586- The patterns described in this document are applied correctly in the implementation.87- Edge cases and anti-patterns listed in this document are avoided.
Run npx get-skill 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. Independent scanners report: SkillSpector: PASS, Skill Scanner: PASS. Capability flags: docs only. 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.