Skip to content

Instantly share code, notes, and snippets.

@coderofsalvation
Created November 16, 2016 13:41
Show Gist options
  • Save coderofsalvation/5c458825570538525dba4e3969f47daa to your computer and use it in GitHub Desktop.
Save coderofsalvation/5c458825570538525dba4e3969f47daa to your computer and use it in GitHub Desktop.
simple javascript function spy
function spy (fn) {
if (!fn) fn = function() {};
function proxy() {
var args = Array.prototype.slice.call(arguments);
proxy.calls.push(args);
proxy.called = true;
fn.apply(this, args);
}
proxy.prototype = fn.prototype;
proxy.calls = [];
proxy.called = false;
return proxy;
}
@Zibri
Copy link

Zibri commented Apr 9, 2019

let's say I want to spy the alert() function how to use this?

@Gustavo10Destroyer
Copy link

@Zibri You can try:
spy(window.alert)

@Zibri
Copy link

Zibri commented Jun 9, 2021

nope

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment