Like builders, factories are a way of organizing object construction. They differ from builders in how they are organized. Often, the interface of factories is a single function call. This makes factories easier to use, if less customizable, than builders.
In this recipe, we'll see how to use factories to easily replicate instances.
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.
- Open your command-line application and navigate to your workspace.
- Create a new folder named
09-03-replicating-instances-with-factories
. - Copy or create an
index.html
that loads and runs amain
function frommain.js
.
- Create a
main.js
file that defines a newclass
namedMission
. Add a constructor that takes aname
constructor argument and assigns it to an instance property. Also, define a simpledescribe
method:
// main.js class Mission { constructor...