Assembling customized instances with builders
The previous recipe shows how to organize the operations of a class. Sometimes, object initialization can also be complicated. In these situations, it can be useful to take advantage of another design pattern: builders.
In this recipe, we'll see how to use builders to organize the initialization of more complicated objects.
Getting ready
This recipe assumes you already have a workspace that allows you to create and run ES modules in your browser. If you don't, please see the first two chapters.
How to do it...
- Open your command-line application and navigate to your workspace.
- Create a new folder named
09-02-assembling-instances-with-builders
.
- Create a
main.js
file that defines a newclass
namedMission
, which that takes aname
constructor argument and assigns it to an instance property. Also, create adescribe
method that prints out some details:
// main.js class Mission { constructor (name) { this.name = name; } describe () { console...