Member Injection

Querying an object to find out what it contains is like asking a restaurant what’s on their menu. The best kind of restaurants I like to frequent are the ones willing to make what I 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. ...

Get Rediscovering JavaScript 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.