Andrew's Digital Garden

Monkey patching

Monkey patching is a technique to add, modify, or suppress the default behavior of a piece of code at runtime without changing its original source code. It can lead to difficult to understand code as the original method is being altered (possibly invisibly), but offers advantages for testing, polyfills, etc.

It can be seen in Ruby, Python, and JavaScript, as some examples.

For example, modifying the original console.log method in JavaScript:

var log = console.log; console.log = function() { // Invoke the original method with an additional parameter log.apply(console, [(new Date()).toString()].concat(arguments)); };

[[concepts]]

Monkey patching