Skip to content

(Patch) All Bar One


Monkey-patching methods in JS classes based on instance attributes

Instance-Dependent Prototype Patching

Posted on by

Sometimes you want to patch a prototype but also let some instances use the original methods. Or maybe you want to mess with people using your code (please don’t).

Here’s a method for selectively patching methods of JavaScript classes. It uses Object.defineProperty so it may not work in all browsers:

  1. Choose your target and save a reference to it somewhere (line 30).
  2. Use Object.defineProperty to define a getter on the class prototype (line 31).
  3. Based your condition (line 33), return either the patched method (line 35) or the original method (line 38).

Of course, once you have a getter like that, you can return all kinds of methods or properties. Bottom line is, you totally can define getter properties on a class prototype and JS will be totally okay with them.

The gist of it, including an ES6 edition that works in Babel.