Can someone give me an example of monkey patching regarding TypeScript & Angular2 and also an explanation ?
-
There is a pretty good explanation of monkey patching in general here: http://stackoverflow.com/questions/5626193/what-is-a-monkey-patch – Seamus Jan 19 '16 at 22:24
2 Answers
Because JavaScript is highly dynamic you can replace a member function (and related functionality) on any object with a new one. It allows you to modify the behaviour of a piece of code without altering the original code.
Here is a simple example in TypeScript:
// original code, assume its in some library
interface Foo {
a:number,
b():number
}
var foo:Foo = {a:123,b:function(){return this.a}}
// Monkey patch the function b with a new one
// Allows you to change the behaviour of foo without changing the original code
foo.b = function(){return 456}
More : Monkey patching is not just interception
When you replace the function but retain the original behaviour, it is function interception. Yes you use monkey patching for function interception (replace the function) but calling the original function is not a requirement for monkey patching.
- The python answer What is monkey patching? (replacement / overriding only)
Also from Wikipedia : https://en.wikipedia.org/wiki/Monkey_patch an application that clearly replaces original behaviour:
Replace methods / classes / attributes / functions at runtime, e.g. to stub out a function during testing;
Finally, just because you can call the original doesn't mean you have to call the original when monkey patching.

- 261,912
- 58
- 460
- 511
-
2I don't think this is monkey patching, it's just the Foo.b function being overridden – Drenai Oct 11 '18 at 09:59
-
3Updated answer. Also overriding is commonly done on inherited instances (no direct mutation). – basarat Mar 03 '20 at 17:58
-
1@Drenai overriding, in the OO sense, would mean creating a new object with the original as its prototype, and redefining the method in the new object. Patching is modifying the behaviour of an existing object and all objects for which it is the prototype, and it replaces the original method - even if it (optionally) calls the original, the whole point of making the patch like this is the change the behaviour without modifying the source code of the library being consumed etc. – Daniel Earwicker Mar 03 '20 at 21:04
The accepted answer is not really an example of monkey patching
. It's an example of overriding a function
The key to monkey patching
is that the original function
is assigned to a variable (for later use), and replaced/overridden with a new function
- but, the new function
has to call the original function
as well
Demo on TypeScript Playground
class Greeter {
greeting: string;
constructor(message: string) {
this.greeting = message;
}
greet() {
return "Hello, " + this.greeting;
}
}
let originalFunciton = Greeter.prototype.greet;
Greeter.prototype.greet = function () {
alert('In Monkey Patched Function');
let returnValue = originalFunciton.apply(this);
return returnValue;
};
let greeter = new Greeter("world");
let button = document.createElement('button');
button.textContent = "Say Hello";
button.onclick = function() {
alert(greeter.greet());
}
document.body.appendChild(button);

- 11,315
- 9
- 48
- 82
-
You'd usually just have `return originalFunciton.apply(this);`, was adding to variable to make it clearer – Drenai May 18 '19 at 06:16
-
3It doesn’t have to call the original function. Frequently a monkey patch will substitute different behaviour for some cases (eg. examine the parameters passed) to decide whether to call the original or do something else. – Daniel Earwicker Mar 03 '20 at 21:00