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 instanceof for type checking
Instructions
Use ES6 class syntax over function constructors for readability.
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 (#field) for data that should not be accessible externally.
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 this — prefer immutable value objects for data
this binding issues arise when class methods are passed as callbacks — use arrow functions or .bind()
Private fields (#field) are not accessible in subclasses without getters
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
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-constructor-pattern3description: JS Constructor Pattern4---5# JS Constructor Pattern67> Use constructor functions or ES6 classes to create and initialize objects89## When to Use1011- You need to create multiple instances of the same type with shared methods12- Object initialization has meaningful logic (validation, default values, derived properties)13- You want to use `instanceof` for type checking1415## Instructions16171. Use ES6 `class` syntax over function constructors for readability.182. Put per-instance data in the constructor (`this.x = ...`).193. Put shared methods on the class body (they go on the prototype automatically).204. Use private class fields (`#field`) for data that should not be accessible externally.2122```javascript23class Rectangle {24 #area = null;2526 constructor(width, height) {27 if (width <= 0 || height <= 0) throw new RangeError('Dimensions must be positive');28 this.width = width;29 this.height = height;30 }3132 getArea() {33 if (this.#area === null) {34 this.#area = this.width * this.height; // memoize35 }36 return this.#area;37 }3839 toString() {40 return `Rectangle(${this.width}x${this.height})`;41 }42}4344const r = new Rectangle(4, 5);45console.log(r.getArea()); // 2046console.log(r instanceof Rectangle); // true47```4849## Details5051The 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.5253**Trade-offs:**5455- Classes encourage mutation via `this` — prefer immutable value objects for data56- `this` binding issues arise when class methods are passed as callbacks — use arrow functions or `.bind()`57- Private fields (`#field`) are not accessible in subclasses without getters5859**When NOT to use:**6061- For simple data bags with no behavior — plain object literals are lighter62- When functional composition (factory functions + closures) better fits the architecture6364## Source6566https://patterns.dev/javascript/constructor-pattern6768## Process69701. Read the instructions and examples in this document.712. Apply the patterns to your implementation, adapting to your specific context.723. Verify your implementation against the details and edge cases listed above.7374## Harness Integration7576- **Type:** knowledge — this skill is a reference document, not a procedural workflow.77- **No tools or state** — consumed as context by other skills and agents.7879## Success Criteria8081- The patterns described in this document are applied correctly in the implementation.82- Edge cases and anti-patterns listed in this document are avoided.
Run npx skillmds@latest add intense-visions/js-constructor-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 Constructor 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.