2

For logging purpose, I've created a wrapper function for an element functions properties. The wrapper function is above:

functionsWrapper: function () {
    for (i = 0, args = new Array(arguments.length); i < arguments.length; i++) {
        args[i] = arguments[i];
    }

    console.log('call for ' + arguments.callee.name + ' with params ' + argumantsLog(args));

    return this['original' + arguments.callee.name].apply(this, args);
}

And then I'm using this code to wrap the element function:

logFunctionsCalls: function (element) {
    if (!element)
        return;

    if (element.children && element.children.length)
        for (var i = 0; i < element.children.length; i++)
            logFunctionsCalls(element.children[i]);

    if (!element.functionsLogged) {
        element.functionsLogged = true;

        for (var property in element) {
            if (typeof element[property] != 'function')
                continue;

            element['original' + property] = element[property];
            element[property] = functionsWrapper;
        }
    }
}

My problem is that the arguments.callee contains the functionsWrapper code without the property name of the called function.

ad-venture
  • 23
  • 3
  • `arguments.callee.name`? That's one deprecated and one non-standard property, and neither will actually help you. – Bergi Sep 01 '16 at 06:37

1 Answers1

1

You cannot use the same functionWrapper everywhere - there's no way to find out as which property it was called. Instead, created different wrappers and keep the original in closure.

for (var p in element) (function(property) {
    if (typeof element[property] != 'function')
        return;

    var original = element[property];
    element[property] = function wrapped() {
        console.log('call for ' + property + ' with params ' + Array.prototype.join.call(arguments));
        return original.apply(this, arguments);
    };
}(p));
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • I've used something similar before the current version, but I must change it because for some reason, the `arguments` object is pointed to irrelevant arguments from other functions call. – ad-venture Sep 01 '16 at 06:51
  • What do you mean by "*pointed to irrelevant arguments from other functions call*"? Please provide some reproducible code example. – Bergi Sep 01 '16 at 06:55
  • For example, I'm getting "src", "http://www.google.com/" that is clearly the passed arguments for `setAttribute` at the function `appendChild` – ad-venture Sep 01 '16 at 09:38
  • Please show us how you are calling those functions. [Edit your question](http://stackoverflow.com/posts/39264210/edit) to include the code. – Bergi Sep 01 '16 at 09:44
  • I don't know why, but my code was pretty the same as yours except the using of inline function, but your code work perfectly. Thanks! – ad-venture Sep 01 '16 at 10:36
  • Well yes, [that IIFE is very important for the closure](http://stackoverflow.com/q/750486/1048572). – Bergi Sep 01 '16 at 11:24