Creating an object using object structuring
Pulling attributes from an object is yet another repetitive task. There seems to be unnecessary repetition. Newer versions of ECMAScript include a syntactic feature that makes this process less onerous. This recipe demonstrates how to use object destructuring to pull new variables from object props.
Getting ready
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.
How to do it...
- Open your command-line application and navigate to your workspace.
- Create a new folder named
06-08-pick-values-from-object-destructuring
. - Copy or create an
index.html
that loads and runs amain
function frommain.js
. - Create a
main.js
file. Create a main function that creates a new object then creates new constants from the properties therein:
// main.js export function main() { const object = { prop1: 'some value', prop2: 'some other value', objectProp...