let's say you have this function:
function foo(){ console.log('foo', arguments);}you can monkey patch it by doing:
function foo(){ console.log('foo', arguments);}foo = function(foo){ // notice that foo here can be named as you like return function(){ // this function is the monkey patched one console.log('before foo'); // by passing `this` we ensure it will work even for methods foo.apply(this, arguments); console.log('after foo'); };}(foo); // you're passing here a reference to the original foo// call itfoo(1, 2, 3);Notice that leaking arguments is not a good idea as it kills performance optimizations (at least in V8,
https://github.com/petkaantonov/bluebird/wiki/Optimization-killers), so it's better to create a copy of it,
the usual solution before ES 2015 was to use `Array.prototype.slice` (in ES 2015 you will use rest and spread and forgot about arguments):
function foo(){ console.log('foo', arguments);}foo = function(foo){ return function(){ console.log('before foo'); foo.apply(this, Array.prototype.slice(arguments)); console.log('after foo'); };}(foo);// call itfoo(1, 2, 3);Of course, this is for answering to your original question,
now if this is a good idea or not depends of why you ask the question,
generically speaking it may not be a good idea,
it's better to create a new function and use the original there,
but if this for other reasons (eg debugging some code) it may be ok.