Member Injection: Instance
Learn how to inject a member into an instance in JavaScript.
We'll cover the following...
Querying an object to find out what it contains is like asking a restaurant what’s on their menu. The kind of restaurants we like to frequent best are the ones willing to make what we like, off the menu, within reason, of course. In other words, you may want to make up stuff in a class that’s not already there. Enter member injection.
📍 You can use member injection to introduce brand-new methods into an object or into the prototype of a class.
The member you add to an object is then available for use on that particular object. The member you inject into a prototype of a class becomes available on all the instances of that class.
Use member injection to add convenience methods or properties that you feel should be in a class. These may be members that are general in nature but were not added by the author of the class.
These members may also be specific to your domain, and having them in the class may make your code more fluent and easy to work with. ...