🌐
DEV Community
dev.to › superviz › design-pattern-7-builder-pattern-10j4
Design Pattern #7 - Builder Pattern - DEV Community
August 8, 2024 - The Builder Pattern is a creational design pattern that allows developers to construct complex objects step-by-step. It separates the construction of an object from its representation, enabling the same construction process to create different ...
🌐
Soham Kamani
sohamkamani.com › javascript › builder-pattern
Using the Builder Pattern in Javascript (With Examples)
Learn how to use the builder pattern in Javascript, and see some code examples, as well as advanced concepts like validation and fixed attribute options.
🌐
DoFactory
dofactory.com › javascript › design-patterns › builder
JavaScript Builder Design Pattern
The Builder pattern allows a client to construct a complex object by specifying the type and content only.
🌐
GeeksforGeeks
geeksforgeeks.org › system design › builder-method-javascript-design-pattern
Builder Method | JavaScript Design Pattern - GeeksforGeeks
July 23, 2025 - Improved Object Creation: The Builder pattern allows for the step-by-step construction of an object, making the creation process more understandable and manageable.
🌐
Medium
medium.com › codex › design-patterns-for-javascript-builder-pattern-c287c54b9b6b
Design Patterns for Javascript — Builder Pattern | by Olutobi Ogunsola | CodeX | Medium
March 25, 2022 - The builder pattern allows us have a base class that we can always refer back to and pick out methods that are always available from the base class, orchestrate their calls and generally come up with a more direct, simple way of constructing the target class.
🌐
Reddit
reddit.com › r/javascript › building objects progressively with the builder pattern in javascript
r/javascript on Reddit: Building objects progressively with the builder pattern in javascript
March 18, 2019 - This new object instance is invalid because we didn't provide a birthDate as a Date object. Since you cannot actually fully use this object until you provide a birthDate, then the builder pattern would not be appropriate here.
🌐
Medium
medium.com › @artemkhrenov › builder-pattern-implementation-in-javascript-ee9f31765aed
Builder Pattern Implementation in JavaScript | by Artem Khrienov | Medium
March 26, 2025 - Builder Pattern Implementation in JavaScript Today, we’re going to dive deep into the Builder Pattern - one of the most practical and widely used design patterns in JavaScript. Whether you’re …
🌐
CodeSignal
codesignal.com › learn › courses › creational-design-patterns-2 › lessons › builder-pattern-in-javascript-step-by-step-object-creation
Builder Pattern in JavaScript | CodeSignal Learn
JavaScript does not have native support for interfaces. Instead, we can document the expected methods a class should implement. This interface specifies the methods that any concrete builder must implement. The buildFoundation, buildStructure, and buildRoof methods represent the steps to construct a house. ... Now, we implement concrete builders following the HouseBuilder pattern...
🌐
Enmascript
enmascript.com › articles › 2019 › 03 › 18 › building-objects-progressively-with-the-builder-pattern-in-javascript
Building objects progressively with the builder pattern in javascript
The builder pattern allows us to create objects gradually as we consume or process information, in this article we'll be understanding how it works with some examples and definitions...
Find elsewhere
🌐
O'Reilly
oreilly.com › library › view › learning-javascript-design › 9781449334840 › ch12s08.html
The Builder Pattern - Learning JavaScript Design Patterns [Book]
July 8, 2012 - The Builder PatternWhen working with the DOM, we often want to construct new elements dynamically—a process that can increase in complexity depending on the final markup,... - Selection from Learning JavaScript Design Patterns [Book]
Author   Addy Osmani
Published   2012
Pages   254
🌐
ZetCode
zetcode.com › javascript › builderpattern
JavaScript Builder Pattern - Object Creation Simplified
Learn how to use the Builder pattern in JavaScript for creating objects, with examples and explanations.
🌐
Everyday
everyday.codes › home › builder pattern in javascript without classes
Builder pattern in JavaScript without classes - everyday.codes
January 28, 2020 - The purpose of the builder pattern is to streamline the object creation process. It is different from the factory pattern: the builder pattern is useful when the creation process is complex and requires an arbitrary number of inputs.
🌐
DZone
dzone.com › coding › javascript › builder pattern in javascript
Builder Pattern in Javascript
September 12, 2019 - Create a method that will return the instance of the class for which the Builder class is made. In our example, we are creating a builder pattern for Course, so we will make a function that will return instance of the Course class with the values.
🌐
Medium
medium.com › @robinviktorsson › a-guide-to-the-builder-design-pattern-in-typescript-and-node-js-with-practical-examples-9e113413ad63
A Guide to the Builder Design Pattern in TypeScript and Node.js with Practical Examples 💻 | by Robin Viktorsson | Medium
March 10, 2025 - The Builder Pattern is a creational design pattern that provides a way to construct complex objects step by step. It provides a clear separation between object creation and its representation, making the code more maintainable and readable.
🌐
Abul Asar's Blog
abulasar.com › understand-and-implement-builder-design-pattern-in-javascript-and-c
Builder Pattern in JavaScript and C++ Explained
January 5, 2025 - Learn how to implement the Builder Design Pattern in JavaScript and C++ for constructing complex objects flexibly and efficiently
🌐
jsmanifest
jsmanifest.com › home › posts › the builder pattern in javascript
The Builder Pattern in JavaScript | jsmanifest
November 20, 2019 - The design pattern we will be going over today is commonly known as the Builder Design Pattern, which is a pattern used to help construct complex objects. When you're developing apps in JavaScript you sometimes find it difficult to construct ...
🌐
Calibraint
calibraint.com › home › blogs › javascript › building objects the right way: understanding the builder pattern in javascript
Decoding The Nuances Of The Builder Design Pattern in JavaScript
August 16, 2024 - The Builder Design Pattern in JavaScript provides a mechanism for “separating the construction of a complex object from its representation,” enabling the same construction process to generate various representations.
🌐
Coderslang
learn.coderslang.com › 0076-javascript-design-patterns-builder
JavaScript Design Patterns - Builder
June 8, 2021 - A step-by-step assembly of the lego pieces at your disposal forms the essence of the Builder pattern. Objects are thus constructed following a sequence of steps one after the other instead of arriving at it all at once. JavaScript objects are a collection of properties and methods.
🌐
Pardanaud
johann.pardanaud.com › blog › modern-builder-design-pattern-in-javascript
Modern Builder design pattern in JavaScript — Blog
This is what the Builder design pattern is meant to do, help the developer build some complex values by providing an extensive API.
Top answer
1 of 1
2

With minimal changes, the WithItem() method can start up an ItemBuilder and chain from it. There needs to be some signifier that the item is finished, which will chain back into the ReceiptBuilder. Here this is a method called endBuildingItem():

function WithItem() {
  this.Items = this.Items || [];
  
  const receiptBuilder = this;
  
  return Object.assign(ItemBuilder(), { 
    endBuildingItem() {
      receiptBuilder.Items.push(this.build());
      return receiptBuilder;
    } 
  });
}

const receipt1 = ReceiptBuilder()
  .WithName('Shopping')
  .WithDate('01-01-01')
  .WithItem()
    .WithPrice(1.99)
    .WithType('eggs')
    .endBuildingItem()
  .build();
  
console.log(receipt1);

const receipt2 = ReceiptBuilder()
  .WithName('Shopping')
  .WithDate('01-01-01')
  .WithItem()
    .WithPrice(2.99)
    .WithType('milk')
    .endBuildingItem()
  .WithItem()
    .WithPrice(3.99)
    .WithType('bread')
    .endBuildingItem()
  .build();
  
console.log(receipt2);

function Receipt(builder) {
  return {
    Name: builder.Name,
    Date: builder.Date,
    Items: builder.Items
  };
}

function ReceiptBuilder() {

  function WithName(name) {
    this.Name = name;
    return this;
  }

  function WithDate(dt) {
    this.Date = dt;
    return this;
  }

  function WithItem() {
    this.Items = this.Items || [];
    
    const receiptBuilder = this;
    
    return Object.assign(ItemBuilder(), { 
      endBuildingItem() {
        receiptBuilder.Items.push(this.build());
        return receiptBuilder;
      } 
    });
  }

  function build() {
    return Receipt(this);
  }

  return {
    WithName,
    WithDate,
    WithItem,
    build
  }
}

function Item(builder) {
  return {
    Type: builder.Type,
    Price: builder.Price
  };
}

function ItemBuilder() {

  function WithType(type) {
    this.Type = type;
    return this;
  }

  function WithPrice(Price) {
    this.Price = Price;
    return this;
  }

  function build() {
    return Item(this);
  }

  return {
    WithPrice,
    WithType,
    build
  }
}
.as-console-wrapper { max-height: 100% !important; }

An alternative implementation would be to put the endBuildingItem() method in ItemBuilder and expect an instance of ReceiptBuilder to be passed into it:

const receipt1 = ReceiptBuilder()
  .WithName('Shopping')
  .WithDate('01-01-01')
  .WithItem()
    .WithPrice(1.99)
    .WithType('eggs')
    .endBuildingItem()
  .build();
  
console.log(receipt1);

const receipt2 = ReceiptBuilder()
  .WithName('Shopping')
  .WithDate('01-01-01')
  .WithItem()
    .WithPrice(2.99)
    .WithType('milk')
    .endBuildingItem()
  .WithItem()
    .WithPrice(3.99)
    .WithType('bread')
    .endBuildingItem()
  .build();
  
console.log(receipt2);

function Receipt(builder) {
  return {
    Name: builder.Name,
    Date: builder.Date,
    Items: builder.Items
  };
}

function ReceiptBuilder() {

  function WithName(name) {
    this.Name = name;
    return this;
  }

  function WithDate(dt) {
    this.Date = dt;
    return this;
  }

  function WithItem() {
    this.Items = this.Items || [];
    
    return ItemBuilder(this);
  }

  function build() {
    return Receipt(this);
  }

  return {
    WithName,
    WithDate,
    WithItem,
    build
  }
}

function Item(builder) {
  return {
    Type: builder.Type,
    Price: builder.Price
  };
}

function ItemBuilder(receiptBuilder) {

  function WithType(type) {
    this.Type = type;
    return this;
  }

  function WithPrice(Price) {
    this.Price = Price;
    return this;
  }
  
  function build() {
    return Item(this);
  }
  
  function endBuildingItem() {
    receiptBuilder.Items.push(this.build());
    return receiptBuilder;
  }

  return {
    WithPrice,
    WithType,
    build,
    endBuildingItem,
  }
}
.as-console-wrapper { max-height: 100% !important; }