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).
Object.defineProperty so it may not work in all browsers:
- Choose your target and save a reference to it somewhere (line 30).
Object.definePropertyto define a getter on the class prototype (line 31).
- 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.