Bundling multiple properties into a single object is another common task in JavaScript that can be really tedious. More recent versions of ECMAScript have added a new bit of syntax to make this more convenient.
In this recipe, we'll see how to use this new syntax to create an object from existing variables.
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
06-09-create-objects-with-structuring
. - Copy or create an
index.html
that loads and runs amain
function frommain.js
. - Create a
main.js
file with anasync
function namedmain
, which creates several constants and then uses object structuring to create an object with properties of the corresponding names and values:
// main.js export function main() { const prop1 = ...