function Dog(name) {
this.name = name;
}
Dog.prototype.bark = function () {
return `${this.name} says woof!`;
};
const d1 = new Dog('Rex');
const d2 = new Dog('Spot');
// Both share the same bark function in memory
console.log(d1.bark === d2.bark); // true
Prefer ES6 class syntax — it uses the prototype chain under the hood but is more readable.
Use hasOwnProperty() or Object.hasOwn() to distinguish own vs inherited properties.
Details
Every JavaScript object has an internal [[Prototype]] link. When you access a property, the engine walks the chain: own properties first, then the prototype, then the prototype's prototype, until null. This is the prototype chain.
Trade-offs:
Shared mutable properties on the prototype are dangerous — if one instance mutates a shared array/object, all instances see the change
The prototype chain adds one lookup level per tier — negligible for most code, but avoid very deep chains
class syntax is clearer than manual .prototype manipulation
When NOT to use:
When instances need truly private state — use closures or ES2022 private class fields (#field) instead
When you need multiple inheritance — JS has single prototype chain; use mixins instead
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-prototype-pattern3description: JS Prototype Pattern4---5# JS Prototype Pattern67> Share properties and methods across instances via the prototype chain89## When to Use1011- You have many instances that should share methods without duplicating them in memory12- You need to extend built-in types or add methods to third-party objects13- You want lightweight objects where method storage is shared, not per-instance1415## Instructions16171. Define shared methods on the constructor's `.prototype` object — not inside the constructor function.182. Instance data (unique per instance) goes on `this` inside the constructor.193. Use `Object.create(proto)` for explicit prototype assignment without a constructor.204. Avoid modifying built-in prototypes (Array, String, Object) — it causes library conflicts.2122```javascript23function Dog(name) {24 this.name = name;25}2627Dog.prototype.bark = function () {28 return `${this.name} says woof!`;29};3031const d1 = new Dog('Rex');32const d2 = new Dog('Spot');3334// Both share the same bark function in memory35console.log(d1.bark === d2.bark); // true36```37385. Prefer ES6 `class` syntax — it uses the prototype chain under the hood but is more readable.396. Use `hasOwnProperty()` or `Object.hasOwn()` to distinguish own vs inherited properties.4041## Details4243Every JavaScript object has an internal `[[Prototype]]` link. When you access a property, the engine walks the chain: own properties first, then the prototype, then the prototype's prototype, until `null`. This is the prototype chain.4445**Trade-offs:**4647- Shared mutable properties on the prototype are dangerous — if one instance mutates a shared array/object, all instances see the change48- The prototype chain adds one lookup level per tier — negligible for most code, but avoid very deep chains49- `class` syntax is clearer than manual `.prototype` manipulation5051**When NOT to use:**5253- When instances need truly private state — use closures or ES2022 private class fields (`#field`) instead54- When you need multiple inheritance — JS has single prototype chain; use mixins instead5556## Source5758https://patterns.dev/javascript/prototype-pattern5960## Process61621. Read the instructions and examples in this document.632. Apply the patterns to your implementation, adapting to your specific context.643. Verify your implementation against the details and edge cases listed above.6566## Harness Integration6768- **Type:** knowledge — this skill is a reference document, not a procedural workflow.69- **No tools or state** — consumed as context by other skills and agents.7071## Success Criteria7273- The patterns described in this document are applied correctly in the implementation.74- Edge cases and anti-patterns listed in this document are avoided.
Run npx skillmds@latest add intense-visions/js-prototype-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 Prototype 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.