Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function factory(lib, ord){
- const result = []
- for (let order of ord){
- const newObj = Object.assign({}, order['template'])
- let funcs = order['parts']
- for (let f of funcs){
- newObj[f] = lib[f]
- }
- result.push(newObj)
- }
- return result
- }
- const library = {
- print: function () {
- console.log(`${this.name} is printing a page`);
- },
- scan: function () {
- console.log(`${this.name} is scanning a document`);
- },
- play: function (artist, track) {
- console.log(`${this.name} is playing '${track}' by ${artist}`);
- },
- };
- const orders = [
- {
- template: { name: "ACME Printer" },
- parts: ["print"],
- },
- {
- template: { name: "Initech Scanner" },
- parts: ["scan"],
- },
- {
- template: { name: "ComTron Copier" },
- parts: ["scan", "print"],
- },
- {
- template: { name: "BoomBox Stereo" },
- parts: ["play"],
- },
- ];
- const products = factory(library, orders);
- console.log(products);
Advertisement
RAW Paste Data
Copied
Advertisement