Indexing arrays directly is relatively simple. The syntax is familiar to all but the most novice developers. However, what is also familiar is off by one error. This means that an array or a collection has been indexed incorrectly by a single position. In some cases, this will cause a fault that is immediately recognizable. Other times, it will cause more subtle errors.
In this recipe, we'll take a look at how to extract members of an array with destructuring syntax.
This recipe assumes that you already have a workspace that allows you to create and run ES modules in your browser. If you don't, refer to the first two chapters.
- Open your command-line application, and navigate to your workspace.
- Create a new folder named
10-06-extract-array-members-with-destructuring
.
- Create a
main.js
file that defines a newclass
namedRocket
that takes a constructor argumentname
and assigns it to an instance property:
// main.js class Rocket...