Skip to content

Instantly share code, notes, and snippets.

@cvazac
Forked from JeremyRH/hijackFunction.js
Created January 21, 2019 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cvazac/ebc544d12dbf58f51ab082810db0cab3 to your computer and use it in GitHub Desktop.
Save cvazac/ebc544d12dbf58f51ab082810db0cab3 to your computer and use it in GitHub Desktop.
const applyBind = Function.prototype.bind.bind(Function.prototype.apply);
const mapSetApply = applyBind(Map.prototype.set);
const hijackedFns = new Map();
function hijackFunction(context, propName, replacer) {
mapSetApply(hijackedFns, [replacer, context[propName]]);
context[propName] = replacer;
}
const fnToStringApply = applyBind(Function.prototype.toString);
const mapGetApply = applyBind(Map.prototype.get);
hijackFunction(Function.prototype, 'toString', function toString() {
return fnToStringApply(mapGetApply(hijackedFns, [this]) || this, arguments);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment