Member Injection: Injecting Multiple Properties
Explore how to add multiple properties at once to JavaScript classes through member injection. Understand how to create read-write properties on prototypes and inject static properties at the class level to extend functionality in modern JavaScript metaprogramming.
We'll cover the following...
Instead of adding one property at a time, we can add multiple properties in one shot—let’s explore how. Along the way, you’ll see how to inject read-write properties. The properties we injected so far were read-only since we created only getters. Next, we’ll create setters as well, so the properties may not only be read but also modified.
Injecting into Array's class
First, let’s think of a couple of useful properties to add to an existing class. The Array class in JavaScript provides a number of nice methods, but there’s no elegant way to access the first or last element. We will inject a first and a last property into the Array class. For this example, let’s start with an array of ...