A.8. Destructuring

Creating instances of objects means constructing them in memory. The term destructuring means changing the structure or taking objects apart. In ES5, you could deconstruct any object or a collection by writing a function to do it. ES6 introduced the destructuring assignment syntax that allows you to extract data from an object’s properties or from an array in a simple expression by specifying a matching pattern. It’s easier to explain by demonstrating an example, which we’ll do next.

A.8.1. Destructuring objects

Let’s say that a getStock() function returns a Stock object that has the attributes symbol and price. In ES5, if you wanted to assign the values of these attributes to separate variables, you’d need to create a variable ...

Get TypeScript Quickly now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.