2

ロギングの目的で、要素関数のプロパティのラッパー関数を作成しました。ラッパー関数は上記のとおりです。

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);
}

そして、このコードを使用して要素関数をラップしています。

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;
        }
    }
}

私の問題は、呼び出された関数のプロパティ名のないコードがarguments.callee含まれていることです。functionsWrapper

4

1 に答える 1

1

どこでも同じものを使用することはできませんfunctionWrapper。どのプロパティとして呼び出されたかを調べる方法はありません。代わりに、別のラッパーを作成し、オリジナルを閉じたままにします。

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));
于 2016-09-01T06:39:05.237 に答える