Back to the lesson

Spy decorator

importance: 5

Create a decorator spy(func) that should return a wrapper that saves all calls to function in its calls property.

Every call is saved as an array of arguments.

For instance:

function work(a, b) {
  alert( a + b ); // work is an arbitrary function or method

work = spy(work);

work(1, 2); // 3
work(4, 5); // 9

for(let args of work.calls) {
  alert( 'call:' + args.join() ); // "call:1,2", "call:4,5"

P.S. That decorator is sometimes useful for unit-testing, it’s advanced form is sinon.spy in Sinon.JS library.

Open the sandbox with tests.

Here we can use calls.push(args) to store all arguments in the log and f.apply(this, args) to forward the call.

Open the solution with tests in the sandbox.