How it works...

In the preceding example, we modified the factory pattern to be a bit more flexible. Rather than calling the factory function directly, we created new ones with the MissionProgramFactoryFn function. In effect, we created a factory of factories!

Variables within a JavaScript function are available to any blocks contained by that function. Thus, the rocketName, destinationName, and payloadName values are available to the body of the factory functions returned from MissionProgramFactoryFn.  This way, we reuse common values for new instances without repeating them.

Returning a function from a function is called a second-order function; this pattern is common in JavaScript.

Get ECMAScript Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.