DEV Community

Nhan Nguyen
Nhan Nguyen

Posted on

JavaScript Design Patterns - Creational - Prototype

Image description

Prototype pattern creates new objects based on an existing one with default property values.

In the example below, we can use the clone() method to create a new object Fruit with the same name and weight as its parent.

class Fruit {
  constructor(name, weight) {
    this.name = name;
    this.weight = weight;
  }

  clone() {
    return new Fruit(this.name, this.weight);
  }
}

export default Fruit;
Enter fullscreen mode Exit fullscreen mode

šŸ‘‰ Use this pattern when:

āž– Our code should not depend on the concrete classes of objects that you need to copy.

āž– We want to reduce the number of subclasses that only differ in how they initialize their respective objects.

šŸš€ Pros:

āž– We can clone objects without coupling to their concrete classes.

āž– We can eliminate repeated initialization code in favor of cloning pre-built prototypes.

āž– We can produce complex objects more conveniently.

āž– We get an alternative to inheritance when dealing with configuration presets for complex objects.

ā›” Cons:

āž– Cloning complex objects that have circular references might be very tricky.


I hope you found it useful. Thanks for reading. šŸ™

Let's get connected! You can find me on:

Tutorial image

Next.js Tutorial 2025 - Build a Full Stack Social App

In this 4-hour hands-on tutorial, Codesistency walks you through the process of building a social platform from scratch with Next.js (App Router), React, Prisma ORM, Clerk for authentication, Neon for PostgreSQL hosting, Tailwind CSS, Shadcn UI, and UploadThing for image uploads.

Watch the full video āž”

Top comments (0)

ACI image

ACI.dev: Fully Open-source AI Agent Tool-Use Infra (Composio Alternative)

100% open-source tool-use platform (backend, dev portal, integration library, SDK/MCP) that connects your AI agents to 600+ tools with multi-tenant auth, granular permissions, and access through direct function calling or a unified MCP server.

Check out our GitHub!