# Passing module settings via closures in JavaScript

Let's say we have the following module:

`;(function(root, type, amount) { let parent = document.querySelector(root); for (let i = 1; i <= amount; i++) { let elem = document.createElement(type); parent.append(elem); } })('#parent', 'p', 5);`

As you can see, three settings are passed into this module: the parent element selector, the element type to create, and the number of elements.

As a rule, such settings are made in the form of an object:

`let config = { root: '#parent', type: 'p', amount: 5 }`

Let's pass our object as a module parameter:

`;(function(config) { let parent = document.querySelector(config.root); for (let i = 1; i <= config.amount; i++) { let elem = document.createElement(config.type); parent.append(elem); } })(config);`

It is more common to destructure an object with settings:

`;(function({root, type, amount}) { let parent = document.querySelector(root); for (let i = 1; i <= amount; i++) { let elem = document.createElement(type); parent.append(elem); } })(config);`
←→
↑
en ru