Using a single factory function to populate an instance is straightforward. In the example below I use the factory function aircraftFactory() to create a new instance called supermarine. However I'm not sure how to structure this so that both aircraftFactory() and engines() could be used together to create supermarine.
"use strict"
function aircraftFactory(x) {
return {
manufacturer: x.manufacturer,
factory: x.factory
}
}
function engines(x) {
return {
numberOfEngines: x.numberOfEngines,
costPerEngine: x.costPerEngine
}
}
let supermarine = aircraftFactory({manufacturer: 'Supermarine', factory: 'Southampton'});
document.querySelector('.output').textContent = supermarine.manufacturer;
<div class='output'></div>
I tried chaining them together like this but it threw an error.
Uncaught TypeError: aircraftFactory(...).engines is not a function
let supermarine = aircraftFactory({manufacturer: 'Supermarine', factory: 'Southampton'}).engines({numberOfEngines: 1, costPerEngine: 35000});
I know there must be a pattern but I can't find an example or figure it out. Thanks for any help!