JS Visitor Pattern
Add new operations to object structures without modifying the objects
When to Use
- You need to perform many unrelated operations on a complex object structure (AST, file tree, DOM)
- Adding new operations should not require changing the element classes
- You want to separate algorithms from the data structures they operate on
Instructions
- Define an
accept(visitor) method on each element class in the object structure.
- Each
accept calls the visitor's corresponding method, passing this (double dispatch).
- Create visitor objects with one method per element type (e.g.,
visitFile, visitFolder).
- New operations = new visitor objects, without modifying element classes.
class File {
constructor(name, size) {
this.name = name;
this.size = size;
}
accept(visitor) {
return visitor.visitFile(this);
}
}
class Folder {
constructor(name, children) {
this.name = name;
this.children = children;
}
accept(visitor) {
return visitor.visitFolder(this);
}
}
const sizeCalculator = {
visitFile(file) {
return file.size;
},
visitFolder(folder) {
return folder.children.reduce((sum, child) => sum + child.accept(this), 0);
},
};
- 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
Source
https://patterns.dev/javascript/visitor-pattern
Process
- 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-pattern-23description: 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.