6
The javascript allows you to create involve functions as can be seen in that SOEN response and in that reply.
With the help of these answers I managed to involve a function spy
, as in the code below:
function spy(func) {
return function(){
var args = Array.prototype.slice.call(arguments);
return func.apply(this, args);
}
}
what I want to do now is this, when I assign this "wrapper" (wrapper), I want it to have an accessible object that generates me a report, for example:
var spied = spy(umaFuncao);
var report = spied.report();
And this object has accessible properties, such as the number of times the method involved has been called, for example:
console.log(report.totalCalls);//Será impresso no console o número de vezes que o método foi chamado
How could I do that? This is a question that can be found in codewars and I thought it would be interesting to bring here.
the path seems to be more or less there, but if I wanted the report, instead of an object being a method that returns an object? I think I misexpressed myself in my question.
– Felipe Avelar
I understood, but in the background not even the object would be necessary... totalCalls could be hung on Spied. But I will edit to do what you asked.
– bfavaretto