There is a simple example of the logging function:
function work(a) { /* ... */ // work - произвольная функция } function makeLogging(f, log) { return function(){ log.push(arguments[0]); f.apply(this,arguments); } } var log = []; work = makeLogging(work, log); work(1); // 1, добавлено в log work(5); // 5, добавлено в log for (var i = 0; i < log.length; i++) { alert( 'Лог:' + log[i] ); // "Лог:1", затем "Лог:5" }
With this code, everything works successfully, according to the comments. Initially, I tried it differently:
function makeLogging(f, log) { return function(){ log=[].slice.call(arguments);// изменения тут f.apply(this,arguments); } }
For obvious reasons, this did not work as planned by the task, but I expected from this code that the log
array will always have the value of the argument of the last function called, however, if after calling work(1)
look at the contents of the log
, it will be empty. I do not fully understand why.