Using destructuring to pick out elements is convenient, but we don't always want to pull out every element. A commonly useful pattern is to get the zeroth element of an array assigned to one variable, and the rest of the elements in another. This is commonly called the head and tail of an array.
In this recipe, we'll take a look at how to use the rest operator to get the head and tail of an array.
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-07-get-head-and-tail-from-array
. - 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 { constructor(name) { this.name = name; } }
- Create...