For functional style, use a switch on type tags instead of double dispatch.
Details
The Visitor pattern achieves the open/closed principle by separating operations from the object structure. New operations (visitors) can be added without modifying existing element classes. This is heavily used in compilers and AST processors (e.g., Babel plugins are visitors).
Trade-offs:
Adding a new element type requires updating all visitors — the pattern favors stable structures with many operations
Double dispatch adds indirection that can be hard to follow
In dynamic JavaScript, a type-tag switch may be simpler than the full visitor ceremony
When NOT to use:
When the object structure changes frequently — every new type breaks every visitor
When there are few operations — just add methods directly to the classes
For simple data — a map/reduce over an array is clearer
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-visitor-pattern3description: JS Visitor Pattern4---5# JS Visitor Pattern67> Add new operations to object structures without modifying the objects89## When to Use1011- You need to perform many unrelated operations on a complex object structure (AST, file tree, DOM)12- Adding new operations should not require changing the element classes13- You want to separate algorithms from the data structures they operate on1415## Instructions16171. Define an `accept(visitor)` method on each element class in the object structure.182. Each `accept` calls the visitor's corresponding method, passing `this` (double dispatch).193. Create visitor objects with one method per element type (e.g., `visitFile`, `visitFolder`).204. New operations = new visitor objects, without modifying element classes.2122```javascript23class File {24 constructor(name, size) {25 this.name = name;26 this.size = size;27 }28 accept(visitor) {29 return visitor.visitFile(this);30 }31}3233class Folder {34 constructor(name, children) {35 this.name = name;36 this.children = children;37 }38 accept(visitor) {39 return visitor.visitFolder(this);40 }41}4243const sizeCalculator = {44 visitFile(file) {45 return file.size;46 },47 visitFolder(folder) {48 return folder.children.reduce((sum, child) => sum + child.accept(this), 0);49 },50};51```52535. For functional style, use a switch on type tags instead of double dispatch.5455## Details5657The Visitor pattern achieves the open/closed principle by separating operations from the object structure. New operations (visitors) can be added without modifying existing element classes. This is heavily used in compilers and AST processors (e.g., Babel plugins are visitors).5859**Trade-offs:**6061- Adding a new element type requires updating all visitors — the pattern favors stable structures with many operations62- Double dispatch adds indirection that can be hard to follow63- In dynamic JavaScript, a type-tag switch may be simpler than the full visitor ceremony6465**When NOT to use:**6667- When the object structure changes frequently — every new type breaks every visitor68- When there are few operations — just add methods directly to the classes69- For simple data — a `map`/`reduce` over an array is clearer7071## Source7273https://patterns.dev/javascript/visitor-pattern7475## Process76771. Read the instructions and examples in this document.782. Apply the patterns to your implementation, adapting to your specific context.793. Verify your implementation against the details and edge cases listed above.8081## Harness Integration8283- **Type:** knowledge — this skill is a reference document, not a procedural workflow.84- **No tools or state** — consumed as context by other skills and agents.8586## Success Criteria8788- The patterns described in this document are applied correctly in the implementation.89- Edge cases and anti-patterns listed in this document are avoided.
Run npx skillmds@latest add intense-visions/js-visitor-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 Visitor 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.