install
source · Clone the upstream repo
git clone https://github.com/Intense-Visions/harness-engineering
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/Intense-Visions/harness-engineering "$T" && mkdir -p ~/.claude/skills && cp -r "$T/agents/skills/claude-code/js-constructor-pattern" ~/.claude/skills/intense-visions-harness-engineering-js-constructor-pattern && rm -rf "$T"
manifest:
agents/skills/claude-code/js-constructor-pattern/SKILL.mdsource content
JS Constructor Pattern
Use constructor functions or ES6 classes to create and initialize objects
When to Use
- You need to create multiple instances of the same type with shared methods
- Object initialization has meaningful logic (validation, default values, derived properties)
- You want to use
for type checkinginstanceof
Instructions
- Use ES6
syntax over function constructors for readability.class - Put per-instance data in the constructor (
).this.x = ... - Put shared methods on the class body (they go on the prototype automatically).
- Use private class fields (
) for data that should not be accessible externally.#field
class Rectangle { #area = null; constructor(width, height) { if (width <= 0 || height <= 0) throw new RangeError('Dimensions must be positive'); this.width = width; this.height = height; } getArea() { if (this.#area === null) { this.#area = this.width * this.height; // memoize } return this.#area; } toString() { return `Rectangle(${this.width}x${this.height})`; } } const r = new Rectangle(4, 5); console.log(r.getArea()); // 20 console.log(r instanceof Rectangle); // true
Details
The Constructor pattern is the foundation of object-oriented JavaScript. ES6 classes are syntactic sugar over prototype-based inheritance —
class bodies define methods on ClassName.prototype, exactly like the older function Constructor() {} approach.
Trade-offs:
- Classes encourage mutation via
— prefer immutable value objects for datathis
binding issues arise when class methods are passed as callbacks — use arrow functions orthis.bind()- Private fields (
) are not accessible in subclasses without getters#field
When NOT to use:
- For simple data bags with no behavior — plain object literals are lighter
- When functional composition (factory functions + closures) better fits the architecture
Source
https://patterns.dev/javascript/constructor-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.