Created
November 16, 2016 13:41
-
-
Save coderofsalvation/5c458825570538525dba4e3969f47daa to your computer and use it in GitHub Desktop.
simple javascript function spy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
let's say I want to spy the alert() function how to use this?