O'Reilly logo

Rediscovering JavaScript by Venkat Subramaniam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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. These ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required